0

我想匹配“错误”的任何变体(假设全面不区分大小写),它后面可能有也可能没有字符,可能有也可能没有“s”。

例如,如果它具有以下内容,则返回匹配的行:

text error: more text
text error more text
text errors more text
text errors( more text
text errors: more text
text errors:more text

但是如果后面有一个等号,我不希望它返回该行。例如:

text errors= more text

基本上,当“错误”或“错误”后面有一个“=”符号时,它总是“错误”。

我想不出更多的东西:

(?i)errors?[^\=]

不区分大小写,字符 e、r、r、o、r,也许还有一个 s,后面不跟一个“=”是我的阅读方式。

1 sample text, more text, errors=123456 more text more text
2 drops=0 link status good errors=0 adapter
3 Error: process failed to start
4 process [ERROR] failed
5 this line might have both ERROR and error=5555 and more text
6 there might be a line that has error=0x02343329 ERROR and more text

我希望返回第 3、4、5 和 6 行,而不是 1 或 2。

没有成功。我在这里先向您的帮助表示感谢。

4

7 回答 7

1

这么多答案,所有答案都如此接近。你想要的是这样的:/error(?!s=)/im

您不需要使用内联组,而是使用 /i 标志。

我可能误解了你的问题,我不确定。但是,如果您也想禁止error=blah,只需使用即可/error(?!s?=)/im

演示+说明:http ://regex101.com/r/oE1eQ9

于 2013-01-12T01:32:17.193 回答
1

所以你想匹配“error”,只要它后面没有“=”或“s=”。这很简单:

/error(?!=|s=)/

你甚至可以把它写成

/error(?!s?=)/

如果你真的想尽可能匹配“错误”(为了设置${^MATCH}或其他东西),你可以使用

/error(?![s=])|errors(?!=)/

您遇到问题是因为[^=]// 匹配“ s”。

于 2013-01-12T04:24:06.140 回答
0

尝试使用前瞻断言:

(?i)errors?(?!\=)
于 2013-01-12T00:55:25.750 回答
0

逻辑 OR 怎么样?

(?i)error[^s=]|errors[^=]
于 2013-01-12T00:58:29.900 回答
0

尝试:

(?i)\berror(?!s?=)

单词锚点,后跟error,只要它后面不跟可选的s然后是等号。

(?!...)是一个否定的前瞻,并且在它不消耗文本的意义上也是一个锚(像^and )(它是一个零宽度的断言)。$

于 2013-01-12T01:29:45.530 回答
0

哦亲爱的。很多人认为他们知道正则表达式!

在你自己的正则表达式(?i)errors?[^\=]中(正如有人所说,=不需要转义,但它没有害处)[^\=]并不完全意味着“后面没有等号”,它的意思是“后面跟着一个不是等号的字符”。error除非在字符串末尾,否则两者相同,因此'error' =~ (?i)errors?[^\=]返回 false。

“不后跟等号”需要负前瞻,所以乍一看它看起来像你想要(?i)errors?(?!=)的,但如果正则表达式引擎发现errors=它会回溯并尝试在没有可选的情况下进行匹配s,看看它是否可以获得整个模式以这种方式匹配,然后会成功,因为errors后面error没有等号。

要解决此问题,您需要无回溯结构(?>...),一旦找到匹配项,就不允许回溯。正则表达式可以满足(?i)(?>errors?)(?!=)您的需要。

最后,为了允许在“不久之后”拒绝等号,error或者errors在它之前需要一些可选的空格,给(?i)(?>errors?)(?!\s*=).

这个程序演示

use strict;
use warnings;

while (<DATA>) {
  chomp;
  printf "%-70s %s\n", $_, /(?i)(?>errors?)(?!\s*=)/ ? 'YES' : 'NO';
}

__DATA__
text error: more text
text error more text
text errors more text
text errors( more text
text errors: more text
text errors:more text
text errors= more tex
text errors = more tex
text error= more tex
text error = more tex
1 sample text, more text, errors=123456 more text more text
2 drops=0 link status good errors=0 adapter
3 Error: process failed to start
4 process [ERROR] failed
5 this line might have both ERROR and error=5555 and more text
6 there might be a line that has error=0x02343329 ERROR and more text

输出

text error: more text                                                  YES
text error more text                                                   YES
text errors more text                                                  YES
text errors( more text                                                 YES
text errors: more text                                                 YES
text errors:more text                                                  YES
text errors= more tex                                                  NO
text errors = more tex                                                 NO
text error= more tex                                                   NO
text error = more tex                                                  NO
1 sample text, more text, errors=123456 more text more text            NO
2 drops=0 link status good errors=0 adapter                            NO
3 Error: process failed to start                                       YES
4 process [ERROR] failed                                               YES
5 this line might have both ERROR and error=5555 and more text         YES
6 there might be a line that has error=0x02343329 ERROR and more text  YES
于 2013-01-12T03:06:50.327 回答
-1

零宽度前瞻是一个很好的答案。尝试:

 (?i)(?!errors?=)error

其中“ (?!errors?=)”的意思是“向前看以检查它是否与“错误?=”不匹配。

更新:这是为了解释问题中原始正则表达式(?i)errors?[^\=]的一个问题:复杂性是“贪婪”(man perlre)如何以及何时不像它可能的那样贪婪,引用:

默认情况下,量化的子模式是“贪婪的”,也就是说,它会尽可能多地匹配(给定一个特定的起始位置),同时仍然允许模式的其余部分匹配。

注意句子的第二部分。原始表达式(?i)errors?[^\=]将匹配“errors=”(或“errors”),因为“s?” 允许匹配0次以允许“[^=]”匹配“s”。这就是为什么它匹配 6 个编号的行。

当这样的匹配(*、+、?和其他)被拒绝时,正则表达式引擎内部发生的事情称为“回溯”,匹配备份并尝试不同的路径以查看是否可以更好地匹配整个正则表达式。独立子表达式 (?>)、环视 (?=) (?!) 及其否定形式可以防止或限制回溯,就像使用交替一样,似乎每个人都有不同的偏好,因为我认为它们中的大多数都出现在某个地方。尽管到目前为止似乎没有人使用“占有” ?+ 形式对您的原始正则表达式进行最小的无回溯更改:

errors?+[^\=]
于 2013-01-12T01:25:23.790 回答