try the following:
import re r = re.compile('(ab|a|1|2)') for i in r.findall('ab1'): print i
The parameter ab was transferred to be the first, so it will match ab in favor of only a . The findall method matches your regular expression more and returns a list of matched groups. In this simple example, you only get a list of strings. Each row for one match. If you had more groups, you will get a list of tuples, each of which contains rows for each group.
This should work for your second example:
pattern = '(7325189|7325|9087|087|18)' str = '7325189087' res = re.compile(pattern).findall(str) print(pattern, str, res, [i for i in res])
I am removing the ^$ characters from the pattern, because if findall has to find more than one substring, then it should search anywhere on str. Then I removed + so that it matches single occurrences of these parameters in the template.
source share