正则表达式匹配字符串而不以模式结束

时间:2022-09-13 07:35:04

I try to find a regex that matches the string only if the string does not end with at least three '0' or more. Intuitively, I tried:

我试图找到一个匹配字符串的正则表达式,只有当该字符串至少以3 '0'结尾时才会匹配该字符串。凭直觉,我试着:

.*[^0]{3,}$

But this does not match when there one or two zeroes at the end of the string.

但是当字符串末尾有一两个0时,这个就不匹配了。

4 个解决方案

#1


13  

If you have to do it without lookbehind assertions (i. e. in JavaScript):

如果您必须不使用lookbehind断言(即在JavaScript中):

^(?:.{0,2}|.*(?!000).{3})$

Otherwise, use hsz's answer.

否则,使用hsz的答案。

Explanation:

解释:

^          # Start of string
(?:        # Either match...
 .{0,2}    #  a string of up to two characters
|          # or
 .*        #  any string
 (?!000)   #   (unless followed by three zeroes)
 .{3}      #  followed by three characters
)          # End of alternation
$          # End of string

#2


7  

You can try using a negative look-behind, i.e.:

你可以试着用消极的目光看后面,例如:

(?<!000)$

Tests:

测试:

Test  Target String   Matches
1     654153640       Yes
2     5646549800      Yes   
3     848461158000    No
4     84681840000     No
5     35450008748     Yes   

Please keep in mind that negative look-behinds aren't supported in every language, however.

不过,请记住,并不是所有的语言都支持负面观点。

#3


2  

What wrong with the no-look-behind, more general-purpose ^(.(?!.*0{3,}$))*$?

no-look-behind错什么,更多的通用^(。(? !。* 0 { 3 } $))* $ ?

The general pattern is ^(.(?!.* + not-ending-with-pattern + $))*$. You don't have to reverse engineer the state machine like Tim's answer does; you just insert the pattern you don't want to match at the end.

一般的模式是^(。(? !。* +无终止模式+ $)*$。你不需要像蒂姆的回答那样对状态机进行逆向设计;您只需插入不想在末尾匹配的模式。

#4


1  

This is one of those things that RegExes aren't that great at, because the string isn't very regular (whatever that means). The only way I could come up with was to give it every possibility.

这是RegExes不太擅长的事情之一,因为字符串不是非常规则的(不管那是什么意思)。我能想到的唯一办法就是给它一切可能。

.*[^0]..$|.*.[^0].$|.*..[^0]$

which simplifies to

它简化了

.*([^0]|[^0].|[^0]..)$

That's fine if you only want strings not ending in three 0s, but strings not ending in ten 0s would be long. But thankfully, this string is a bit more regular than some of these sorts of combinations, and you can simplify it further.

这很好,如果你只需要字符串不以0结尾,但是字符串不会在10个0中结束会很长。但值得庆幸的是,这个字符串比这些组合中的一些更有规则性,你可以进一步简化它。

.*[^0].{0,2}$

#1


13  

If you have to do it without lookbehind assertions (i. e. in JavaScript):

如果您必须不使用lookbehind断言(即在JavaScript中):

^(?:.{0,2}|.*(?!000).{3})$

Otherwise, use hsz's answer.

否则,使用hsz的答案。

Explanation:

解释:

^          # Start of string
(?:        # Either match...
 .{0,2}    #  a string of up to two characters
|          # or
 .*        #  any string
 (?!000)   #   (unless followed by three zeroes)
 .{3}      #  followed by three characters
)          # End of alternation
$          # End of string

#2


7  

You can try using a negative look-behind, i.e.:

你可以试着用消极的目光看后面,例如:

(?<!000)$

Tests:

测试:

Test  Target String   Matches
1     654153640       Yes
2     5646549800      Yes   
3     848461158000    No
4     84681840000     No
5     35450008748     Yes   

Please keep in mind that negative look-behinds aren't supported in every language, however.

不过,请记住,并不是所有的语言都支持负面观点。

#3


2  

What wrong with the no-look-behind, more general-purpose ^(.(?!.*0{3,}$))*$?

no-look-behind错什么,更多的通用^(。(? !。* 0 { 3 } $))* $ ?

The general pattern is ^(.(?!.* + not-ending-with-pattern + $))*$. You don't have to reverse engineer the state machine like Tim's answer does; you just insert the pattern you don't want to match at the end.

一般的模式是^(。(? !。* +无终止模式+ $)*$。你不需要像蒂姆的回答那样对状态机进行逆向设计;您只需插入不想在末尾匹配的模式。

#4


1  

This is one of those things that RegExes aren't that great at, because the string isn't very regular (whatever that means). The only way I could come up with was to give it every possibility.

这是RegExes不太擅长的事情之一,因为字符串不是非常规则的(不管那是什么意思)。我能想到的唯一办法就是给它一切可能。

.*[^0]..$|.*.[^0].$|.*..[^0]$

which simplifies to

它简化了

.*([^0]|[^0].|[^0]..)$

That's fine if you only want strings not ending in three 0s, but strings not ending in ten 0s would be long. But thankfully, this string is a bit more regular than some of these sorts of combinations, and you can simplify it further.

这很好,如果你只需要字符串不以0结尾,但是字符串不会在10个0中结束会很长。但值得庆幸的是,这个字符串比这些组合中的一些更有规则性,你可以进一步简化它。

.*[^0].{0,2}$