Access a group within a group using Regex

I am trying to find a regular expression that groups a word that ends with two identical characters followed by 'ter' and splits it into two characters. Example: the word "letter" should be grouped into "Let" and "ter". I am using python, and this is what I got 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 (\ w) \ 1 does not belong to the right group, since it is a group inside the group. How to solve this?

Thanks in advance.

+6
source share
2 answers

I use named groups, which makes their link 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'} 
+5
source

I want the first group to be everything before and including the first of two identical characters, the second group is the second identical character followed by 'er'

It will be:

 match = re.search(r'(\w*(\w)(?=\2))(\w*er$)', str) print match.groups() # -> ('Let', 't', 'ter') 
+1
source

Source: https://habr.com/ru/post/944762/


All Articles