Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Match on any Number Except

Tags:

regex

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.

like image 444
Brandon Wilson Avatar asked Jan 29 '18 15:01

Brandon Wilson


People also ask

How to exclude something with regex?

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.

What is\\ d in regex?

\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).

What are word characters in regex?

A word character is a character a-z, A-Z, 0-9, including _ (underscore).


1 Answers

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).*

like image 130
The fourth bird Avatar answered Oct 07 '22 20:10

The fourth bird