Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word between 9-10 characters, of which 0-2 are numbers

Tags:

regex

http://regexr.com?32uvo

What I currently have:

\b(?=[A-Z\d]{10})(?:[A-Z]*\d){0,2}[A-Z]*\b

This would only match a string with a length of 10. I would like to change it to between 9 and 10 characters, where 2 can be numbers. Why doesn't this work?

\b(?=[A-Z\d]{9,10})(?:[A-Z]*\d){0,2}[A-Z]*\b

AFAIK, {9,10} should be the length interval.

like image 582
Johan Avatar asked Nov 03 '22 10:11

Johan


1 Answers

You were close

 \b(?=[A-Z\d]{9,10}\b)(?:[A-Z]*\d){0,2}[A-Z]*\b
                   --            
                    |->you missed this     

try it here

So this regex would match a word that contains 9 to 10 characters[upper case and digits] that contain 1 to 2 digits


if you want to match the whole string you better use ^(start of the string) and $(end of the string)

like image 180
Anirudha Avatar answered Nov 15 '22 11:11

Anirudha