Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex string to find numbers not starting with 91

Tags:

regex

I'm trying to narrow the following regular expression:

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

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

like image 748
user77413 Avatar asked May 18 '11 05:05

user77413


2 Answers

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

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

(?!91) causes the pattern to match only if the next two characters are not 91, but does not consume those characters, leaving them to be matched by [0-9]{22}.

Many regexp engines also allow \d for decimal digits. If yours does, then:

/\b((?!91)\d{22})\b/
like image 133
Wayne Conrad Avatar answered Sep 25 '22 00:09

Wayne Conrad


Try this:

/\b(?:[0-8][0-9]|9[02-9])[0-9]{20}\b/
like image 37
Kobi Avatar answered Sep 24 '22 00:09

Kobi