简体   繁体   中英

regex to match everything except 2 digits

I'm having a problem with regex which matches everything except 2 digits in the middle.

Example:

Input : 40442**75**22123456

Match : 75

Allow: 4044273xxxxxxxx , 4044255xxxx etc..

The expression I have is: [0-9]{5}[0-68-9]{1}[0-46-9]{1}[0-9]{8}

It works for exact match but fails for exception cases.

If you just want to allow 75 in that spot, use an alternation.
Put all your exceptions in an alternation. There will be overlap,
but there will be some not allowed.
If it's a case where the ones not allowed are few, then use a general
range with a negative assertion, to disallow some. Ie [0-9]{2} (?<!52|74)

[0-9]{5}(?:[0-68-9][0-46-9]|75)[0-9]{8}

Expanded

 [0-9]{5} 
 (?:
      [0-68-9] 
      [0-46-9] 
   |  75
   |  [0-9]5
   |  etc...
 )
 [0-9]{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