Similar to questions How to capture multiple repeated groups?, or How to iterate over the matched groups of a regular expression, but I can't seem to wrap my head around if its slightly different.. Seems like most folks want to cycle through the instances of the matches within the string, rather than cycle through the Groups that the string matches.
I have a compiled regular expression, with multiple groups in it - each with an OR between them.. My regular expression looks like this:
self.combined_re = re.compile('|(?P<a0>/T/R/E/Relation)|(?P<a1>/T/R/E/Relation)|(?P<a2>/T/R/E/Relation/Orig)|(?P<a3>/T/R/E/Relation)|(?P<a4>/T/R/E/Relation/Related)|(?P<a5>/T/R/E/Relation/Role)|(?P<a6>/T/R/E/Relation/Volume)')
Note that the above is an example - my regular expressions are dynamically created, but this is just an example. Its a slightly more obvious regular expression with only exact strings, for the sake of simplicity..
If I run
match_object = self.combined_re.fullmatch("/T/R/E/Relation")
you can see that groups a0, a1, and a3 will ALL match this one string...
How can I cycle through these 3 groups (not knowing they will be the ones matched)? Ugly pseudo code to try and show an example as to how I'd use it:
for each matched group in match_object:
if matched group = a0:
do a0 stuff
elsif matched group = a1:
do a1 stuff
...
elsif matched group = a6:
do a6 suff
I am currently using the
match_object.lastgroup
call, but of course this just gives me one of the groups and I lose the others. Is there no way to do this with match objects? I would really prefer not to change my re .. hoping this can be done with some match object things I am missing.. Thanks!