I see lots of people are using pipes, but that seems to only match the first instance. If you want to match all, then try using lookaheads.
Example:
>>> fruit_string = "10a11p" >>> fruit_regex = r'(?=.*?(?P<pears>\d+)p)(?=.*?(?P<apples>\d+)a)' >>> re.match(fruit_regex, fruit_string).groupdict() {'apples': '10', 'pears': '11'} >>> re.match(fruit_regex, fruit_string).group(0) '10a,11p' >>> re.match(fruit_regex, fruit_string).group(1) '11'
(?= ...) is a look ahead:
Matches if ... matches next, but doesn’t consume any of the string. This is called a lookahead assertion. For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
.*?(?P<pears>\d+)p find a number followed a p anywhere in the string and name the number "pears"