正则表达式相反 [英] Regular Expression Opposite

查看:60
本文介绍了正则表达式相反的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写一个正则表达式来返回所需结果的相反结果?正则表达式通常是包容性的 - 查找匹配项.我希望能够将正则表达式转换为它的对立面 - 断言没有匹配项.这可能吗?如果是这样,如何?

Is it possible to write a regex that returns the converse of a desired result? Regexes are usually inclusive - finding matches. I want to be able to transform a regex into its opposite - asserting that there are no matches. Is this possible? If so, how?

http://zijab.blogspot.com/2008/09/finding-opposite-of-regular-expression.html 指出你应该用

/^((?!^ MYREGEX ).)*$/

,但这似乎不起作用.如果我有正则表达式

, but this doesn't seem to work. If I have regex

/[a|b]./

,字符串abc"在我的正则表达式和 zijab 建议的相反情况下都返回 false,

, the string "abc" returns false with both my regex and the converse suggested by zijab,

/^((?!^[a|b].).)*$/

.是否可以编写正则表达式的对话,还是我的想法有误?

. Is it possible to write a regex's converse, or am I thinking incorrectly?

推荐答案

倒转正则表达式不起作用的原因是负前瞻中的^":

The reason your inverted regex isn't working is because of the '^' inside the negative lookahead:

/^((?!^[ab].).)*$/
      ^            # WRONG

也许它在 vim 中有所不同,但在我熟悉的每种正则表达式中,脱字符匹配字符串的开头(或多行模式中的行开头).但我认为这只是博客条目中的一个错字.

Maybe it's different in vim, but in every regex flavor I'm familiar with, the caret matches the beginning of the string (or the beginning of a line in multiline mode). But I think that was just a typo in the blog entry.

您还需要考虑您正在使用的正则表达式工具的语义.例如,在 Perl 中,这是正确的:

You also need to take into account the semantics of the regex tool you're using. For example, in Perl, this is true:

"abc" =~ /[ab]./

但在 Java 中,这不是:

But in Java, this isn't:

"abc".matches("[ab].")

那是因为传递给 matches() 方法的正则表达式隐式锚定在两端(即 /^[ab].$/).

That's because the regex passed to the matches() method is implicitly anchored at both ends (i.e., /^[ab].$/).

采用更常见的 Perl 语义,/[ab]./ 表示目标字符串包含一个由 'a' 或 'b' 后跟至少一个(非行分隔符)字符.换句话说,在任何时候,条件为 TRUE.该语句的反面是,在每个点条件为 FALSE.这意味着,在您使用每个字符之前,您执行否定前瞻以确认该字符不是匹配序列的开头:

Taking the more common, Perl semantics, /[ab]./ means the target string contains a sequence consisting of an 'a' or 'b' followed by at least one (non-line separator) character. In other words, at ANY point, the condition is TRUE. The inverse of that statement is, at EVERY point the condition is FALSE. That means, before you consume each character, you perform a negative lookahead to confirm that the character isn't the beginning of a matching sequence:

(?![ab].).

并且您必须检查每个字符,因此必须在两端锚定正则表达式:

And you have to examine every character, so the regex has to be anchored at both ends:

/^(?:(?![ab].).)*$/

这是一般的想法,但我认为不可能反转每个正则表达式——当原始正则表达式可以包括正面和负面的环顾、不情愿和占有量词以及谁知道时-什么.

That's the general idea, but I don't think it's possible to invert every regex--not when the original regexes can include positive and negative lookarounds, reluctant and possessive quantifiers, and who-knows-what.

这篇关于正则表达式相反的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆