Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match string not ending with pattern

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:

.*[^0]{3,}$

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

like image 816
Jelena Avatar asked Jul 11 '12 11:07

Jelena


People also ask

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol. Example: "^a" matches "a" at the start of the string.

What does \b mean in regex?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a “word boundary”. This match is zero-length. There are three different positions that qualify as word boundaries: Before the first character in the string, if the first character is a word character.

What is the regex pattern for end of string?

End of String or Line: $ The $ anchor specifies that the preceding pattern must occur at the end of the input string, or before \n at the end of the input string. If you use $ with the RegexOptions. Multiline option, the match can also occur at the end of a line.

How do I match a pattern in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .


1 Answers

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.

like image 78
hsz Avatar answered Oct 30 '22 07:10

hsz