Regex到*not*不匹配任何字符

时间:2022-09-08 10:34:47

I know it is quite some weird goal here but for a quick and dirty fix for one of our system we do need to not filter any input and let the corruption go into the system.

我知道这是一个很奇怪的目标,但是对于我们这个系统的一个快速而肮脏的修复,我们确实需要不过滤任何输入,让腐败进入系统。

My current regex for this is "\^.*"

我现在的正则表达式是“\ ^ *”。

The problem with that is that it does not match characters as planned ... but for one match it does work. The string that make it not work is ^@jj (basically anything that has ^ ... ).

问题是它没有按照计划匹配字符……但对于一场比赛来说,它确实有效。字符串,使它不工作是^ @jj(基本上任何^……)。

What would be the best way to not match any characters now ? I was thinking of removing the \  but only doing this will transform the "not" into a "start with" ...

现在不匹配任何字符的最佳方式是什么?我在考虑去掉\,但只有这样做才能把“not”变成“start with”……

9 个解决方案

#1


61  

The ^ character doesn't mean "not" except inside a character class ([]). If you want to not match anything, you could use a negative lookahead that matches anything: (?!.*).

^字符并不意味着“不”除了在一个字符类([])。如果您不想匹配任何内容,您可以使用一个与任何内容都匹配的负面前视:(?!.*)。

#2


42  

A simple and cheap regex that will never match anything is to match against something that is simply unmatchable, for example: \b\B.

一个简单而廉价的regex将永远不会匹配任何东西,它将匹配一些根本无法匹配的东西,例如:\b\ b。

It's simply impossible for this regex to match, since it's a contradiction.

这个regex根本不可能匹配,因为它是矛盾的。

References

  • regular-expressions.info\Word Boundaries
    • \B is the negated version of \b. \B matches at every position where \b does not.
    • \B是\B的否定版本。B \B在B \不匹配的位置匹配。
  • 信息\单词边界B是\B的否定版本。B \B在B \不匹配的位置匹配。

#3


10  

Another very well supported and fast pattern that would fail to match anything that is guaranteed to be constant time:

另一种非常受支持且快速的模式将无法匹配任何保证为常数时间的东西:

$unmatchable pattern $anything goes here etc.

$unmatchable模式$所有东西到这里等等。

$ of course indicates the end-of-line. No characters could possibly go after $ so no further state transitions could possibly be made. The additional advantage are that your pattern is intuitive, self-descriptive and readable as well!

$当然表示行尾。没有字符可能追求$,因此不可能进行进一步的状态转换。额外的好处是您的模式是直观的,自我描述的和可读的!

#4


1  

Instead of trying to not match any characters, why not just match all characters? ^.*$ should do the trick. If you have to not match any characters then try ^\j$ (Assuming of course, that your regular expression engine will not throw an error when you provide it an invalid character class. If it does, try ^()$. A quick test with RegexBuddy suggests that this might work.

与其尝试不匹配任何字符,为什么不匹配所有字符呢?^。*$应该可以。如果你需要不匹配任何字符然后试着^ \ j $(当然,假设你的正则表达式引擎不会抛出错误时提供一个无效的字符类。如果是这样,试着^()美元。使用RegexBuddy进行快速测试表明,这可能是可行的。

#5


0  

^ is only not when it's in class (such as [^a-z] meaning anything but a-z). You've turned it into a literal ^ with the backslash.

^不只是在课堂上的时候(比如^[a - z]意义除了a - z)。你把它变成文字^反斜杠。

What you're trying to do is [^]*, but that's not legal. You could try something like

你正在试图做的是[^]*,但这不是合法的。你可以试试

" {10000}"

which would match exactly 10,000 spaces, if that's longer than your maximum input, it should never be matched.

这将恰好匹配10,000个空格,如果这比最大输入长,就不应该匹配。

#6


0  

((?iLmsux))

Try this, it matches only if the string is empty.

试试这个,它只在字符串为空时匹配。

#7


-1  

You want to match nothing at all? Neg lookarounds seems obvious, but can be slow, perhaps ^$ (matches empty string only) as an alternative?

你什么都不想匹配?Neg看看似乎是显而易见的,但可能会很慢,也许^ $(仅匹配空字符串)作为替代吗?

#8


-2  

Have you tried this simple regex? [^.]*

你试过这个简单的regex吗?[^]*

#9


-2  

Eh I know this is a little late, but you could simply not read any input if the regex is empty

嗯,我知道有点晚了,但是如果regex是空的,您就不能读取任何输入

#1


61  

The ^ character doesn't mean "not" except inside a character class ([]). If you want to not match anything, you could use a negative lookahead that matches anything: (?!.*).

^字符并不意味着“不”除了在一个字符类([])。如果您不想匹配任何内容,您可以使用一个与任何内容都匹配的负面前视:(?!.*)。

#2


42  

A simple and cheap regex that will never match anything is to match against something that is simply unmatchable, for example: \b\B.

一个简单而廉价的regex将永远不会匹配任何东西,它将匹配一些根本无法匹配的东西,例如:\b\ b。

It's simply impossible for this regex to match, since it's a contradiction.

这个regex根本不可能匹配,因为它是矛盾的。

References

  • regular-expressions.info\Word Boundaries
    • \B is the negated version of \b. \B matches at every position where \b does not.
    • \B是\B的否定版本。B \B在B \不匹配的位置匹配。
  • 信息\单词边界B是\B的否定版本。B \B在B \不匹配的位置匹配。

#3


10  

Another very well supported and fast pattern that would fail to match anything that is guaranteed to be constant time:

另一种非常受支持且快速的模式将无法匹配任何保证为常数时间的东西:

$unmatchable pattern $anything goes here etc.

$unmatchable模式$所有东西到这里等等。

$ of course indicates the end-of-line. No characters could possibly go after $ so no further state transitions could possibly be made. The additional advantage are that your pattern is intuitive, self-descriptive and readable as well!

$当然表示行尾。没有字符可能追求$,因此不可能进行进一步的状态转换。额外的好处是您的模式是直观的,自我描述的和可读的!

#4


1  

Instead of trying to not match any characters, why not just match all characters? ^.*$ should do the trick. If you have to not match any characters then try ^\j$ (Assuming of course, that your regular expression engine will not throw an error when you provide it an invalid character class. If it does, try ^()$. A quick test with RegexBuddy suggests that this might work.

与其尝试不匹配任何字符,为什么不匹配所有字符呢?^。*$应该可以。如果你需要不匹配任何字符然后试着^ \ j $(当然,假设你的正则表达式引擎不会抛出错误时提供一个无效的字符类。如果是这样,试着^()美元。使用RegexBuddy进行快速测试表明,这可能是可行的。

#5


0  

^ is only not when it's in class (such as [^a-z] meaning anything but a-z). You've turned it into a literal ^ with the backslash.

^不只是在课堂上的时候(比如^[a - z]意义除了a - z)。你把它变成文字^反斜杠。

What you're trying to do is [^]*, but that's not legal. You could try something like

你正在试图做的是[^]*,但这不是合法的。你可以试试

" {10000}"

which would match exactly 10,000 spaces, if that's longer than your maximum input, it should never be matched.

这将恰好匹配10,000个空格,如果这比最大输入长,就不应该匹配。

#6


0  

((?iLmsux))

Try this, it matches only if the string is empty.

试试这个,它只在字符串为空时匹配。

#7


-1  

You want to match nothing at all? Neg lookarounds seems obvious, but can be slow, perhaps ^$ (matches empty string only) as an alternative?

你什么都不想匹配?Neg看看似乎是显而易见的,但可能会很慢,也许^ $(仅匹配空字符串)作为替代吗?

#8


-2  

Have you tried this simple regex? [^.]*

你试过这个简单的regex吗?[^]*

#9


-2  

Eh I know this is a little late, but you could simply not read any input if the regex is empty

嗯,我知道有点晚了,但是如果regex是空的,您就不能读取任何输入