我怎样才能“反向匹配"?用正则表达式? [英] How can I "inverse match" with regex?
问题描述
我正在逐行处理一个文件,我想做一个反向匹配.例如,我想匹配包含六个字母的字符串的行,但前提是这六个字母不是Andrea".我该怎么做?
I'm processing a file, line-by-line, and I'd like to do an inverse match. For instance, I want to match lines where there is a string of six letters, but only if these six letters are not 'Andrea'. How should I do that?
我正在使用 RegexBuddy,但仍有问题.
I'm using RegexBuddy, but still having trouble.
推荐答案
(?!Andrea).{6}
假设您的正则表达式引擎支持负前瞻...
Assuming your regexp engine supports negative lookaheads...
...或者您可能更喜欢使用 [A-Za-z]{6}
代替 .{6}
...or maybe you'd prefer to use [A-Za-z]{6}
in place of .{6}
请注意,前瞻和后视通常不是逆向"的正确方法.正则表达式匹配.正则表达式并不是真正为进行负匹配而设置的;他们将其留给您使用它们的任何语言.
Note that lookaheads and lookbehinds are generally not the right way to "inverse" a regular expression match. Regexps aren't really set up for doing negative matching; they leave that to whatever language you are using them with.
这篇关于我怎样才能“反向匹配"?用正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!