Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex in Python to get string of numbers after string of letters

Tags:

python

regex

I have a string formatted as results_item12345. The numeric part is either four or five digits long. The letters will always be lowercase and there will always be an underscore somewhere in the non-numeric part.

I tried to extract it using the following:

 import re
 string = 'results_item12345'
 re.search(r'[^a-z][\d]',string)

However, I only get the leftmost two digits. How can I get the entire number?

like image 745
mac389 Avatar asked Nov 23 '25 02:11

mac389


1 Answers

Assuming you only care about the numbers at the end of the string, the following expression matches 4 or 5 digits at the end of the string.

\d{4,5}$

Otherwise, the following would be the full regex matching the provided requirements.

^[a-z_]+\d{4,5}$
like image 98
Jason McCreary Avatar answered Nov 25 '25 15:11

Jason McCreary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!