user3656665
user3656665

Reputation: 117

Create regex phone number validating all number except one format

I have a regex which authorized all phone number in the format :

0nxxxxxxxx with n between 1-9 and 8 times x between 0-9

My regex is

0[1-9][0-9]{8}

Now, I want to exclude the number which begin by 0590xxxxxx. So, for example

0123456789 => true
...        => true
0590123456 => false

How can I modify my regex to do this ?

Upvotes: 0

Views: 65

Answers (1)

karthik manchala
karthik manchala

Reputation: 13640

Just add negative lookahead (?!0590) before your pattern:

(?!0590)0[1-9][0-9]{8}

See RegexStorm Demo

Improvements:

  • I suggest you to use start ^ and end $ anchors if you want to avoid matches like abc0123456789abc or use word boundaries \b
  • you can replace [0-9] with \d

Improved regex:

^(?!0590)0[1-9]\d{8}$

Upvotes: 1

Related Questions