如何否定整个正则表达式?
我有一个正则表达式,例如 (ma|(t){1})
。它匹配 ma
和 t
,但不匹配 bla
。
我想否定正则表达式,因此它必须匹配 bla
而不是 ma
和 t
,通过向此正则表达式添加一些内容。我知道我可以编写 bla ,但实际的正则表达式更复杂。
I have a regex, for example (ma|(t){1})
. It matches ma
and t
and doesn't match bla
.
I want to negate the regex, thus it must match bla
and not ma
and t
, by adding something to this regex. I know I can write bla
, the actual regex is however more complex.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用负环视:
(?!
pattern
)
正环视可用于断言模式匹配。否定环视则相反:它用于断言模式不匹配。有些风格支持断言;有些则支持断言。有些对lookbehind等进行了限制。
链接到regular-expressions.info
另请参阅
更多示例
这些是尝试提出玩具问题的正则表达式解决方案作为练习;如果您想学习使用环视的各种方法(嵌套它们、使用它们捕获等),它们应该具有教育意义:
Use negative lookaround:
(?!
pattern
)
Positive lookarounds can be used to assert that a pattern matches. Negative lookarounds is the opposite: it's used to assert that a pattern DOES NOT match. Some flavor supports assertions; some puts limitations on lookbehind, etc.
Links to regular-expressions.info
See also
More examples
These are attempts to come up with regex solutions to toy problems as exercises; they should be educational if you're trying to learn the various ways you can use lookarounds (nesting them, using them to capture, etc):
假设您只想禁止完全匹配正则表达式的字符串(即
mmbla
可以,但mm
不行),这就是您想要的:( ?!(?:m{2}|t)$)
是一个负 lookahead< /a>;它表示“从当前位置开始,接下来的几个字符不是mm
或t
,后面是字符串的结尾。”开头的起始锚点 (^
) 确保在字符串的开头应用前瞻。如果成功,.*
会继续并使用该字符串。仅供参考,如果您使用 Java 的
matches()
方法,则实际上并不需要^
和最后的$
,但它们不要造成任何伤害。不过,前瞻中的$
是必需的。Assuming you only want to disallow strings that match the regex completely (i.e.,
mmbla
is okay, butmm
isn't), this is what you want:(?!(?:m{2}|t)$)
is a negative lookahead; it says "starting from the current position, the next few characters are notmm
ort
, followed by the end of the string." The start anchor (^
) at the beginning ensures that the lookahead is applied at the beginning of the string. If that succeeds, the.*
goes ahead and consumes the string.FYI, if you're using Java's
matches()
method, you don't really need the the^
and the final$
, but they don't do any harm. The$
inside the lookahead is required, though.这是给定的正则表达式。
\b 是寻找单词边界。
积极的前瞻 (?=\w) 是为了避免空格。
对原始正则表达式的负面展望是为了防止它的匹配。
最后 (\w*) 是捕获剩下的所有单词。
保存单词的组是组 3。
简单的 (?!pattern) 将不起作用,因为任何子字符串都会匹配
简单的 ^(?!(?:m{2}|t)$).*$ 将不起作用,因为它的粒度是整行
this is for the given regex.
the \b is to find word boundary.
the positive look ahead (?=\w) is here to avoid spaces.
the negative look ahead over the original regex is to prevent matches of it.
and finally the (\w*) is to catch all the words that are left.
the group that will hold the words is group 3.
the simple (?!pattern) will not work as any sub-string will match
the simple ^(?!(?:m{2}|t)$).*$ will not work as it's granularity is full lines
以下内容在 JavaScript 中有效:
例如,在您的情况下,您的否定正则表达式将是
^(?![\s\S]*(ma|(t){1}))[\s\S] *$。
下面的
negate
函数可用于将正则表达式转换为其否定形式:The following works in JavaScript:
So for example, in your case, your negated regex would be
^(?![\s\S]*(ma|(t){1}))[\s\S]*$
.The
negate
function below can be used to convert a regex into its negated form:这个正则表达式计算你的条件:
看看它是如何工作的:
https://regex101.com/r/Ryg2FX/1
This regexp math your condition:
Look at how it works:
https://regex101.com/r/Ryg2FX/1
如果您使用 Laravel,请应用此命令。
Laravel 有一个 not_regex ,其中验证的字段必须与给定的正则表达式不匹配;在内部使用 PHP
preg_match
函数。Apply this if you use laravel.
Laravel has a not_regex where field under validation must not match the given regular expression; uses the PHP
preg_match
function internally.