I have this regex that I am trying to match on any except the following number
0|1|2|3|4|5|6
^(?!(0|1|2|3|4|5|6)).*
I can get it to match on 7 or 8 or 9, but 10 doesn't work nor does anything after since they start with a number that I don't want to match on.
For example, if my number is 22, then it would match. If my number is 2, then it wouldn't match.
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
\d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ). \s (space) matches any single whitespace (same as [ \t\n\r\f] , blank, tab, newline, carriage-return and form-feed).
A word character is a character a-z, A-Z, 0-9, including _ (underscore).
I think you can update your regex adding a word boundary \b
after the group to:
^(?!(0|1|2|3|4|5|6)\b).*
You could also write this shorter replacing the or statements with a character range from 0-6 like:
^(?![0-6]\b).*
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With