Regex string to find numbers not starting with 91

I am trying to narrow down the following regex:

/\b([0-9]{22})\b/

to match only 22-digit numbers that don't start with "91". Does anyone know how to do this?

+5
source share
2 answers

If your regexp engine has zero width negative view , then:

/\b((?!91)[0-9]{22})\b/

(?!91)forces the pattern to match only if the next two characters are not 91, but do not consume these characters, leaving them under the appropriate ones [0-9]{22}.

Many regexp mechanisms also allow \dfor decimal digits. If you do this, then:

/\b((?!91)\d{22})\b/
+6
source

:

/\b(?:[0-8][0-9]|9[02-9])[0-9]{20}\b/
+2

All Articles