Go regex to match all lines that don't start with timestamp

huangapple go评论63阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2017年5月2日 01:57:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/43723905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定