Search code examples
regexjsl

Regex match between n and m numbers but as much as possible


I have a set of strings that have some letters, occasional one number, and then somewhere 2 or 3 numbers. I need to match those 2 or 3 numbers. I have this:

\w*(\d{2,3})\w*

but then for strings like

AAA1AAA12A
AAA2AA123A

it matches '12' and '23' respectively, i.e. it fails to pick the three digits in the second case. How do I get those 3 digits?


Solution

  • Looks like the correct answer would be:

        \w*?(\d{2,3})\w*
    

    Basically, making preceding expression lazy does the job