Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python regex match only last occurrence

I'm experiencing some problems implementing a regular expression for a repeating string pattern.

>>> re.findall('(\(\w+,\d+\)(?:,)?)+', '(a,b),(c,d),(e,f)')
['(e,f)']

I would like ro get the other items as well

Help would be really appriciated

like image 444
CodeNinja Avatar asked Jun 27 '26 13:06

CodeNinja


1 Answers

Remove the +; your pattern matches all occurrences, but the group can only capture one occurrence, you cannot repeat a capturing group that way:

>>> import re
>>> re.findall('(\(\w+,\w+\),?)+', '(a,b),(c,d),(e,f)')
['(e,f)']
>>> re.findall('\(\w+,\w+\),?', '(a,b),(c,d),(e,f)')
['(a,b),', '(c,d),', '(e,f)']

where I replaced the \d with \w to demonstrate, and removed the redundant non-capturing group around the comma. The outermost capturing group is also redundant; without it, re.findall() returns the whole matched expression.

like image 164
Martijn Pieters Avatar answered Jun 29 '26 01:06

Martijn Pieters



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!