哦亲爱的。很多人认为他们知道正则表达式!
在你自己的正则表达式(?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