如何否定整个正则表达式?

发布于 2024-08-29 08:26:28 字数 253 浏览 5 评论 0原文

我有一个正则表达式,例如 (ma|(t){1})。它匹配 mat,但不匹配 bla

我想否定正则表达式,因此它必须匹配 bla 而不是 mat通过向此正则表达式添加一些内容。我知道我可以编写 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清旖 2024-09-05 08:26:28

使用负环视:(?!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):

漫雪独思 2024-09-05 08:26:28

假设您只想禁止完全匹配正则表达式的字符串(即 mmbla 可以,但 mm 不行),这就是您想要的:

^(?!(?:m{2}|t)$).*$

( ?!(?:m{2}|t)$) 是一个负 lookahead< /a>;它表示“从当前位置开始,接下来的几个字符不是 mmt,后面是字符串的结尾。”开头的起始锚点 (^) 确保在字符串的开头应用前瞻。如果成功,.* 会继续并使用该字符串。

仅供参考,如果您使用 Java 的 matches() 方法,则实际上并不需要 ^ 和最后的 $,但它们不要造成任何伤害。不过,前瞻中的 $ 是必需的。

Assuming you only want to disallow strings that match the regex completely (i.e., mmbla is okay, but mm isn't), this is what you want:

^(?!(?:m{2}|t)$).*$

(?!(?:m{2}|t)$) is a negative lookahead; it says "starting from the current position, the next few characters are not mm or t, 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.

风铃鹿 2024-09-05 08:26:28
\b(?=\w)(?!(ma|(t){1}))\b(\w*)

这是给定的正则表达式。
\b 是寻找单词边界。
积极的前瞻 (?=\w) 是为了避免空格。
对原始正则表达式的负面展望是为了防止它的匹配。
最后 (\w*) 是捕获剩下的所有单词。
保存单词的组是组 3。
简单的 (?!pattern) 将不起作用,因为任何子字符串都会匹配
简单的 ^(?!(?:m{2}|t)$).*$ 将不起作用,因为它的粒度是整行

\b(?=\w)(?!(ma|(t){1}))\b(\w*)

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

︶ ̄淡然 2024-09-05 08:26:28

以下内容在 JavaScript 中有效:

^(?![\s\S]*(?:ORIGINAL_REGEX_SOURCE))[\s\S]*$

例如,在您的情况下,您的否定正则表达式将是 ^(?![\s\S]*(ma|(t){1}))[\s\S] *$。

下面的 negate 函数可用于将正则表达式转换为其否定形式:

function negate(regex) {
    return new RegExp(
        String.raw`^(?![\s\S]*(?:${regex.source}))[\s\S]*

以下内容在 JavaScript 中有效:

^(?![\s\S]*(?:ORIGINAL_REGEX_SOURCE))[\s\S]*$

例如,在您的情况下,您的否定正则表达式将是 ^(?![\s\S]*(ma|(t){1}))[\s\S] *$。

下面的 negate 函数可用于将正则表达式转换为其否定形式:

, regex.flags, ) } const tests = [ { regex: /(ma|(t){1})/, matches: ['ma', 't'], nonMatches: ['bla'] }, { regex: /foo/, matches: ['foo'], nonMatches: ['bar'] }, { regex: /(foo|bar)/, matches: ['foo', 'bar'], nonMatches: ['baz'] }, { regex: /\d{3}/, matches: ['123', '456', '999'], nonMatches: ['foo'] }, ] const results = [] function check(condition, message = 'Condition failed') { if (!condition) { throw new Error(message) } results.push(message) } for (const { regex, matches, nonMatches } of tests) { for (const text of matches) { const atStart = `${text} ...` const atEnd = `... ${text}` const inMiddle = `... ${text} ...` check(regex.test(text), `${regex} matches ${JSON.stringify(text)}`) check(regex.test(atStart), `${regex} matches ${JSON.stringify(atStart)}`) check(regex.test(atEnd), `${regex} matches ${JSON.stringify(atEnd)}`) check(regex.test(inMiddle), `${regex} matches ${JSON.stringify(inMiddle)}`) const negated = negate(regex) check(!negated.test(text), `${negated} doesn't match ${JSON.stringify(text)}`) check(!negated.test(atStart), `${negated} doesn't match ${JSON.stringify(atStart)}`) check(!negated.test(atEnd), `${negated} doesn't match ${JSON.stringify(atEnd)}`) check(!negated.test(inMiddle), `${negated} doesn't match ${JSON.stringify(inMiddle)}`) const doubleNegated = negate(negated) check(doubleNegated.test(text), `${doubleNegated} matches ${JSON.stringify(text)}`) check(doubleNegated.test(atStart), `${doubleNegated} matches ${JSON.stringify(atStart)}`) check(doubleNegated.test(atEnd), `${doubleNegated} matches ${JSON.stringify(atEnd)}`) check(doubleNegated.test(inMiddle), `${doubleNegated} matches ${JSON.stringify(inMiddle)}`) } for (const text of nonMatches) { const atStart = `${text} ...` const atEnd = `... ${text}` const inMiddle = `... ${text} ...` check(!regex.test(text), `${regex} doesn't match ${JSON.stringify(text)}`) check(!regex.test(atStart), `${regex} doesn't match ${JSON.stringify(atStart)}`) check(!regex.test(atEnd), `${regex} doesn't match ${JSON.stringify(atEnd)}`) check(!regex.test(inMiddle), `${regex} doesn't match ${JSON.stringify(inMiddle)}`) const negated = negate(regex) check(negated.test(text), `${negated} matches ${JSON.stringify(text)}`) check(negated.test(atStart), `${negated} matches ${JSON.stringify(atStart)}`) check(negated.test(atEnd), `${negated} matches ${JSON.stringify(atEnd)}`) check(negated.test(inMiddle), `${negated} matches ${JSON.stringify(inMiddle)}`) const doubleNegated = negate(negated) check(!doubleNegated.test(text), `${doubleNegated} doesn't match ${JSON.stringify(text)}`) check(!doubleNegated.test(atStart), `${doubleNegated} doesn't match ${JSON.stringify(atStart)}`) check(!doubleNegated.test(atEnd), `${doubleNegated} doesn't match ${JSON.stringify(atEnd)}`) check(!doubleNegated.test(inMiddle), `${doubleNegated} doesn't match ${JSON.stringify(inMiddle)}`) } } console.info(['', ...results].join('\n')) console.info('All tests passed')

The following works in JavaScript:

^(?![\s\S]*(?:ORIGINAL_REGEX_SOURCE))[\s\S]*$

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:

function negate(regex) {
    return new RegExp(
        String.raw`^(?![\s\S]*(?:${regex.source}))[\s\S]*

The following works in JavaScript:

^(?![\s\S]*(?:ORIGINAL_REGEX_SOURCE))[\s\S]*$

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:

, regex.flags, ) } const tests = [ { regex: /(ma|(t){1})/, matches: ['ma', 't'], nonMatches: ['bla'] }, { regex: /foo/, matches: ['foo'], nonMatches: ['bar'] }, { regex: /(foo|bar)/, matches: ['foo', 'bar'], nonMatches: ['baz'] }, { regex: /\d{3}/, matches: ['123', '456', '999'], nonMatches: ['foo'] }, ] const results = [] function check(condition, message = 'Condition failed') { if (!condition) { throw new Error(message) } results.push(message) } for (const { regex, matches, nonMatches } of tests) { for (const text of matches) { const atStart = `${text} ...` const atEnd = `... ${text}` const inMiddle = `... ${text} ...` check(regex.test(text), `${regex} matches ${JSON.stringify(text)}`) check(regex.test(atStart), `${regex} matches ${JSON.stringify(atStart)}`) check(regex.test(atEnd), `${regex} matches ${JSON.stringify(atEnd)}`) check(regex.test(inMiddle), `${regex} matches ${JSON.stringify(inMiddle)}`) const negated = negate(regex) check(!negated.test(text), `${negated} doesn't match ${JSON.stringify(text)}`) check(!negated.test(atStart), `${negated} doesn't match ${JSON.stringify(atStart)}`) check(!negated.test(atEnd), `${negated} doesn't match ${JSON.stringify(atEnd)}`) check(!negated.test(inMiddle), `${negated} doesn't match ${JSON.stringify(inMiddle)}`) const doubleNegated = negate(negated) check(doubleNegated.test(text), `${doubleNegated} matches ${JSON.stringify(text)}`) check(doubleNegated.test(atStart), `${doubleNegated} matches ${JSON.stringify(atStart)}`) check(doubleNegated.test(atEnd), `${doubleNegated} matches ${JSON.stringify(atEnd)}`) check(doubleNegated.test(inMiddle), `${doubleNegated} matches ${JSON.stringify(inMiddle)}`) } for (const text of nonMatches) { const atStart = `${text} ...` const atEnd = `... ${text}` const inMiddle = `... ${text} ...` check(!regex.test(text), `${regex} doesn't match ${JSON.stringify(text)}`) check(!regex.test(atStart), `${regex} doesn't match ${JSON.stringify(atStart)}`) check(!regex.test(atEnd), `${regex} doesn't match ${JSON.stringify(atEnd)}`) check(!regex.test(inMiddle), `${regex} doesn't match ${JSON.stringify(inMiddle)}`) const negated = negate(regex) check(negated.test(text), `${negated} matches ${JSON.stringify(text)}`) check(negated.test(atStart), `${negated} matches ${JSON.stringify(atStart)}`) check(negated.test(atEnd), `${negated} matches ${JSON.stringify(atEnd)}`) check(negated.test(inMiddle), `${negated} matches ${JSON.stringify(inMiddle)}`) const doubleNegated = negate(negated) check(!doubleNegated.test(text), `${doubleNegated} doesn't match ${JSON.stringify(text)}`) check(!doubleNegated.test(atStart), `${doubleNegated} doesn't match ${JSON.stringify(atStart)}`) check(!doubleNegated.test(atEnd), `${doubleNegated} doesn't match ${JSON.stringify(atEnd)}`) check(!doubleNegated.test(inMiddle), `${doubleNegated} doesn't match ${JSON.stringify(inMiddle)}`) } } console.info(['', ...results].join('\n')) console.info('All tests passed')

臻嫒无言 2024-09-05 08:26:28

这个正则表达式计算你的条件:

^.*(?<!ma|t)$

看看它是如何工作的:
https://regex101.com/r/Ryg2FX/1

This regexp math your condition:

^.*(?<!ma|t)$

Look at how it works:
https://regex101.com/r/Ryg2FX/1

拥抱影子 2024-09-05 08:26:28

如果您使用 Laravel,请应用此命令。

Laravel 有一个 not_regex ,其中验证的字段必须与给定的正则表达式不匹配;在内部使用 PHP preg_match 函数。

'email' => 'not_regex:/^.+$/i'

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.

'email' => 'not_regex:/^.+$/i'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文