匹配不带负向先行断言的带有连字符的四位数字的正则表达式。

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

Regular expression on negate of 4 digits appended with a hyphen without using negative lookahead

问题

我尝试了许多正则表达式的可能解决方案,以识别不以4位数字和连字符开头的行,而不使用负向前瞻。我正在尝试在filebeat中配置的正则表达式来识别不以年份和连字符开头的行。一个示例可以是2023-

一个使用负向前瞻的有效正则表达式是^((?!(^[0-9]{4}-$)).)*$,可以用作开始识别问题的方法。有关如何在不使用负向前瞻的情况下完成的任何帮助?

英文:

I have tried with many possible solutions of regex to identify lines that do not start with 4 digits and a hyphen without using negative lookahead. The regex I am trying to configure in filebeat to identify lines that do not start with year and a hyphen. An example can be 2023-.

One regex with negative lookahead which works is as ^((?!(^[0-9]{4}-$)).)*$ which can be used as a start to identify the problem. Any help regarding this that could be done without the negative lookahead.?

答案1

得分: 1

第一个选择项匹配一个字符串,其中前4个字符中至少有一个不是数字。第二个选择项匹配4个数字,后面不跟着连字符。

英文:
^(?:.{0,3}(?:[^\d\n]|$)|\d{4}(?:[^-\n]|$)).*

The first alternative matches a string where one of the first 4 characters isn't a digit. The second alternative matches 4 digits not followed by a hyphen.

DEMO

huangapple
  • 本文由 发表于 2023年7月17日 20:58:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704723.html
匿名

发表评论

匿名网友

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

确定