I am trying to find a regex that groups a word that ends on two identical symbols followed by 'ter' and splits it on the two symbols. Example: The word 'Letter' should be grouped into 'Let' and 'ter'. I'm using python and this is what i've gotten so far:
match = re.search(r'(\w*)((\w)\1(er$))', str)
print match.group(1) #should print 'Let'
print match.group(2) #should print 'ter'
The problem is that the (\w)\1 doesn't refer to the right group, because it's a group inside a group. How is this solved?
Thanks in advance.
I'm using named groups as that makes referencing them easier:
import re
pattern = r"""
\b(?P<first_part>\w*(?P<splitter>\w)) # matches starting at a word boundary
(?P<last_part>(?P=splitter)er\b) # matches the last letter of the first group
# plus 'er' if followed by a word boundary
"""
matcher = re.compile(pattern, re.X)
print matcher.search('letter').groupdict()
# out: {'first_part': 'let', 'last_part': 'ter', 'splitter': 't'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With