I am implementing a method that takes in a regex pattern like r'(\w+/)+end'
and a string 'ab/cd/ef/end'
. Note that I cannot request the caller of the method to update their pattern format. Within the method, I needs to perform an operation that requires extracting all matches of the first capturing group i.e. ab/
, cd/
, and ef/
.
How do I accomplish this in Python? Something like below returns a tuple of last-matches for each of capturing groups. We have just one in this example, so it returns ('ef/',)
.
re.match(r'(\w+/)+end', 'ab/cd/ef/end').groups()
By the way, in C#, every capturing group can match multiple strings e.g. Regex.Match("ab/cd/ef/end", @"(\w+/)+end").Groups[1].Captures
will return all the three matches for first capturing group (\w+/)+
.