As per Documentation here:
'|'
A|B
, where A and B can be arbitrary REs, creates a regular expression that will match either A or B. An arbitrary number of REs can be separated by the'|'
in this way. This can be used inside groups (see below) as well. As the target string is scanned, REs separated by'|'
are tried from left to right. When one pattern completely matches, that branch is accepted. This means that once A matches, B will not be tested further, even if it would produce a longer overall match. In other words, the'|'
operator is never greedy. To match a literal'|'
, use\|
, or enclose it inside a character class, as in[|]
.
So I tried this and got expected result. My regex A(a.*b) found match, so regex B(b.*e) is not in result(even though it will produce a longer match).
>> re.findall('a.*b|b.*e', 'abcdef')
>> ['ab']
But then, how does this work? Even though regex A(b.*d) can find match, result of regex B(a.*e) is in the output.
>> re.findall('b.*d|a.*e', 'abcdef')
>> ['abcde']
Here I am not trying to parse any string with regex but just trying to understand how regex works. So there is no expected output or sample output.