According to the python doc, vertical bars literal are used as an 'or' operator. It matches A|B,where A and B can be arbitrary REs.
For example, if the regular expression is as following: ABC|DEF,it matches strings like these:
"ABC", "DEF"
But what if I want to match strings as following:
"ABCF", "ADEF"
Perhaps what I want is something like A(BC)|(DE)F which means:
I know the above expression is not right since brackets have other meanings in regular expression, just to express my idea.
Thanks!
These will work:
A(BC|DE)F
A(?:BC|DE)F
The difference is the number of groups generated: 1 with the first, 0 with the second.
Yours will match either ABC
or DEF
, with 2 groups, one containing nothing and the other containing the matched fragment (BC
or DE
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With