简体   繁体   中英

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 ?

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}$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM