Find a part number with len 5 or 6using re.findall

Hello All…i need a quick help…i got text and from the text i want to extract the part number.
The part number have 5 and sometimes have 6 numbers.

text = "partnumber_123456"
re.findall(r"_\w{5}", text)

output: 12345


I tried:
text = "partnumber_123456"
re.findall(r"_\w{5}|_\w{6}", text)

output: 12345

Can we add OR logic to re.finall that will output 5 or 6 numbers. Examplce code above onlt output ‘12345’…thanks in advance

You should be using an online regex tool to help with this.

Hint: both regexes are hitting but it’s only taking the first

Or better yet

Can you not simply use re.findall('[0-9]', text)?

{5,6} will match between 5 and 6 of something.

1 Like

Thank you…It works…Bless you ")

{5} and {6} expect specific, exact numbers of matches. We have many more tools to quantify the matches:

? means zero or one (an optional match)
* means any number (zero or more)
+ means at least one (one or more)

We can also use a comma inside {} to make a range of possibilities. (However, we cannot normally put a space in between the {}; that will break the special meaning of the {}.)

{5,6} means either 5 or 6 (between 5 and 6, inclusive. We are not listing each possible option, but instead beginning and end points for a range.)
{,6} means up to 6 (between 0 and 6, inclusive)
{5,} means 5 or more

So, the ? quantifier means the same thing as {0,1} (or {,1}); * means the same thing as {0,} (in fact we can even use {,}); + means the same thing as {1,}.

A regex looks for the first match. Given _\w{5}|_\w{6}, it’ll try the first alternative (_\w{5}) before the second alternative (_\w{6}), so if there’s a ‘_’ followed by 6 (or more) digits, it’ll match the ‘_’ and the first 5 digits.

The fix is to put the longer match first, i.e. _\w{6}|_\w{5}, or , as others have said, _\w{5,6}.

1 Like