Regular expression excluding subpatterns

I have codes corresponding to a very simple string scheme: XXnnnnnnnnn (2 alphanumeric, 9 numeric) .

I use this regex: \w{2}\d{9} .

Now I need to refuse to match any string starting with the constant token ' AY ', and any string with 11 repeated characters (for example, ' 11111111111 ' or ' 00000000000 ').

How can I exclude subpatterns using regular expressions?

+4
source share
2 answers

try it

 \b(?!AY)(?!(\w)\1{10})\w{2}\d{9}\b 

See here at Regexr

I basically added only \b word boundaries to your regular expression to avoid partial matches.

Your limitations are achieved through the use of negative statements about forecasts.

(?!AY) The statement fails if the pattern begins with "AY"

(?!(\w)\1{10}) The statement fails if the character of the first word is repeated 10 more times.

Lookaround statements at regular-expressions.info

+5
source

You can use zero-width matches to exclude these two cases.

Not suitable AY:

  (?!AY) 

Does not match 11 identical characters. You either need to use backlinks (this means that the regular expression is not strictly regular anymore), or you can explicitly point to each digit.

 (?!0{11}|1{11}|2{11}|…|9{11}) // 11 zeros, or 11 ones, or 11 twos, … (?!(.)\1{10}) // A character, followed by itself 10 more times. 

So this will do full regexp:

 (?!AY)(?!(.)\1{10})\w{2}\d{9} 
+2
source

Source: https://habr.com/ru/post/1399967/


All Articles