0

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 ?

1

1 Answer 1

1

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}$
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I didn't know the negative lookahead. I need the ^ and $ anchors like you suggest. The problem is when I add them to the pattern, it never matches with a correct input RegexStormDemo
Yes.. something is wrong.. but it will work.. checked with another online tester :)
Indeed, it works, it's exactly what I needed. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.