user3287037 user3287037 - 24 days ago 8
Python Question

Return multiple match groups

Trying to find a way to return 'A','B', and 'C'.

I've tried using the following regex:

{{(?:\|\[\[([a-zA-Z\s]+)+\]\])+}}


for the string:

{{|[[A]]|[[B]]|[[C]]}}


but so far little success.

Many thanks.

Answer

Note that ([a-zA-Z\s]+)+ is clearly a human error - there is no point in quantifying the group that has a single atom that is already +-quantified.

If you can use PyPi regex module you could use your slightly fixed regex and just access Group 1 captures:

>>> import regex
>>> pat = r"{{(?:\|\[\[([a-zA-Z\s]+)]])+}}"
>>> s = "{{|[[A]]|[[B]]|[[C]]}}"
>>> res = regex.fullmatch(pat, s)
>>> print(res.captures(1))
['A', 'B', 'C']

Else, you will have to use a 2-step approach: 1) check if the whole string matches the pattern you need, and 2) use \|\[\[([a-zA-Z\s]+)]] with re.findall to get multiple required matches.

>>> res = []
>>> if re.search(r"^{{(?:\|\[\[([a-zA-Z\s]+)]])+}}$", s):
    res = re.findall(r"\|\[\[([a-zA-Z\s]+)]]", s)
>>> print(res)
['A', 'B', 'C']