Search code examples
pythonregexdigits

Regex to capture word with at least one number in it


I'm almost done with all my regex stuff but i encounter another problem, i have this regex :

(?=.*\d)[A-Z0-9]{5,}

It captures all stuff i need as :

AP51711

And it works but sometimes it has a strange behaviour, as far as i understood regex (i'm noob :p ) my regex is supposed to capture things that contains at least one DIGIT !

But on this string :

3M BUFFING MACHINE P64392

The output will be :

['BUFFING', 'MACHINE', 'P64392']

I don't understand why 'BUFFING' and 'MACHINE' are captured :O

If someone could help me understand this, thanks !


Solution

  • if you do that:

     (?=[A-Z]*\d)[A-Z0-9]{5,}
    

    you have the result waited...