-1

I want to capture repeated groups in Python as a separate list:

match = re.match(r'!((?:abc|123)+)!', '!abc123abc!').groups()
print(match)
print(len(match))

This gives back a tuple with a single element:

('abc123abc',)
1

How can I get the following output?

('abc', '123', 'abc',)
3

Following this helpful article on capturing repeated groups I now understand the earlier problem I had, trying to repeat a capturing group instead of capturing a repeated group. But still I don't understand how or if it is possible to capture different groups for better post-processing.

Please note that I cannot do without the pre-/suffix, because this also contains multiple capturing groups. My actual use case differs a little bit from this MWE, but should be clear enough.

Felix
  • 1,837
  • 9
  • 26

1 Answers1

0

This resembles the correct answer by @WiktorStribiżew while staying more easily comparable to my original question

import regex as re
match = re.match(r'!(abc|123)+!', '!abc123abc!').captures(1)
print(match)
print(len(match))

Which correctly outputs

['abc', '123', 'abc']
3

This works, because the regex module (not to be confused with Python's internal re module) handles groups differently when using the capture() method, i.e. not overwriting them (as outlined in the article in my original question), but rather append them, not overwriting the index. This can be followed in the section Notes on named capture groups in the regex package's official documentation.

Felix
  • 1,837
  • 9
  • 26