Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regular Expression -- not matching digits at end of string

This will be really quick marks for someone...

Here's my string:

Jan 13.BIGGS.04222 ABC DMP 15

I'm looking to match:

  1. the date at the front (mmm yy) format
  2. the name in the second field
  3. the digits at the end. There could be between one and three.

Here is what I have so far:

(\w{3} \d{2})\.(\w*)\..*(\d{1,3})$

Through a lot of playing around with http://www.pythonregex.com/ I can get to matching the '5', but not '15'.

What am I doing wrong?

like image 964
Blue Otter Hat Avatar asked Dec 10 '25 21:12

Blue Otter Hat


2 Answers

Use .*? to match .* non-greedily:

In [9]: re.search(r'(\w{3} \d{2})\.(\w*)\..*?(\d{1,3})$', text).groups()
Out[9]: ('Jan 13', 'BIGGS', '15')

Without the question mark, .* matches as many characters as possible, including the digit you want to match with \d{1,3}.

like image 63
unutbu Avatar answered Dec 13 '25 09:12

unutbu


Alternatively to what @unutbu has proposed, you can also use word boundary \b - this matches "word border":

(\w{3} \d{2})\.(\w*)\..*\b(\d{1,3})$

From the site you referred:

>>> regex = re.compile("(\w{3} \d{2})\.(\w*)\..*\b(\d{1,3})$")
>>> regex.findall('Jan 13.BIGGS.04222 ABC DMP 15')
[(u'Jan 13', u'BIGGS', u'15')]
like image 43
Tadeck Avatar answered Dec 13 '25 09:12

Tadeck



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!