What's a regex that matches all numbers except 1, 2 and 25?

ktulinho picture ktulinho · Aug 28, 2014 · Viewed 16k times · Source

There's an input of Strings that are composed of only digits, i.e. integer numbers. How to write a regex that will accept all the numbers except numbers 1, 2 and 25?

I want to use this inside the record identification of BeanIO (which supports regex) to skip some records that have specific values.

I reach this point ^(1|2|25)$, but I wanted the opposite of what this matches.

Answer

Joseph Silber picture Joseph Silber · Aug 28, 2014

Not that a regex is the best tool for this, but if you insist...

Use a negative lookahead:

/^(?!(?:1|2|25)$)\d+/

See it here in action: http://regexr.com/39df2