Regex - Revert a specific line / sequence of numbers [java]

I'm having trouble finding a RegEx that negates a specific string. In this case, I am dealing with numbers .

If I want to exclude the number "12" from my group of numbers:

[1, 12, 121, 212, 312]

      

How can I do this using RegEx? If I use something like ^ ((! 12).) * $ It will exclude all numbers except "1" because they all have the pattern "12".

What would be the rigth expression to use in this case?

+3


source to share


3 answers


^((?!\b12\b).)*$

      



This should do it for you. Boundary boundaries will only allow you to exclude 12

and not others.

+4


source


You don't need a regex for this, you can simply:



String num = "[1, 12, 121, 212, 312]".split(",")[1].trim();

      

0


source


You can just use this,

^(?!.*\\b12\\b)\\d+$

      

The negative look at the start states that there is a number ahead and not followed by a 12

word boundary in the lines that will match.

0


source







All Articles