我有这个正则表达式:
<(\d+)>(\w+\s\d+\s\d+(?::\d+){2})\s([\w\/\.\-]*)(.*)
Run Code Online (Sandbox Code Playgroud)
如果第三组是"MSWinEventLog"并返回"匹配",则我想要做的是返回FALSE(不匹配).
<166>Apr 28 10:46:34 AMC the remaining phrase
<11>Apr 28 10:46:34 MSWinEventLog the remaining phrase
<170>Apr 28 10:46:34 Avantail the remaining phrase
<171>Apr 28 10:46:34 Avantail the remaining phrase
<172>Apr 28 10:46:34 AMC the remaining phrase
<173>Apr 28 10:46:34 AMC the remaining phrase
<174>Apr 28 10:46:34 Avantail the remaining phrase
<175>Apr 28 10:46:34 AMC the remaining phrase
<176>Apr 28 10:46:34 AMC the remaining phrase
<177>Apr 28 10:46:34 Avantail the remaining phrase
<178>Apr 28 10:46:34 AMC the remaining phrase
<179>Apr 28 10:46:34 Avantail the remaining phrase
<180>Apr 28 10:46:34 Avantail the remaining phrase
Run Code Online (Sandbox Code Playgroud)
如何在正则表达式组中加入" NOT " MSWinEventLog' ([\w\/\.\-]*) ?
注意:
上面的第二个短语应该返回"不匹配"
<(\d+)>(\w+\s\d+\s\d+(?::\d+){2})\s(?!MSWinEventLog)([\w\/\.\-]*)(.*)
Run Code Online (Sandbox Code Playgroud)
一个负前瞻(这里为' (?!MSWinEventLog)")应该足够:
如果你想要匹配其他东西不匹配的东西,那么否定的先行是必不可少的.
在解释字符类时,我已经解释了为什么你不能使用否定字符类来匹配"q"而不是"u".否定前瞻提供了解决方案:q(?!u).