英文:
Go regex to match all lines that don't start with timestamp
问题
有人能解释一下正确的Java正则表达式是什么,以匹配所有不以时间戳[0-9]{4}-[0-9]{2}-[0-9]{2}
开头的行吗?
我试过使用^(^[0-9]{4}-[0-9]{2}-[0-9]{2})
,但它不起作用。
英文:
Can anybody explain what the correct Java regex is to match all lines that don't start with timestamp [0-9]{4}-[0-9]{2}-[0-9]{2}
?
I am trying to use ^(^[0-9]{4}-[0-9]{2}-[0-9]{2})
but it doesn't work.
答案1
得分: 1
你的 ^(^[0-9]{4}-[0-9]{2}-[0-9]{2})
模式匹配以你定义的模式开头的字符串(这里的 ^
只匹配字符串的开头)。
在 Go 语言中,正则表达式引擎不支持向前查找,因此很难创建一个可读性好的正则表达式来完成所需的任务。
我建议你删除所有与你的模式匹配的行
(?m)\s*^[0-9]{4}-[0-9]{2}-[0-9]{2}.*
(查看示例),然后使用换行符分割结果,以获取未匹配模式的行。
英文:
Your ^(^[0-9]{4}-[0-9]{2}-[0-9]{2})
pattern matches a string starting with the pattern you defined (the ^
here just matches the start of a string).
In Go lang, the regex engine does not support lookarounds, and thus it is difficult to create a readable regex that would do the required job.
I suggest you remove all lines that match your pattern
(?m)\s*^[0-9]{4}-[0-9]{2}-[0-9]{2}.*
(see demo) and then split the result with line breaks to get the lines that did not match the pattern.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论