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:
- match “A” first,
- then string “BC” or “DE”,
- then char “F”.
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:
The difference is the number of groups generated: 1 with the first, 0 with the second.
Yours will match either
ABCorDEF, with 2 groups, one containing nothing and the other containing the matched fragment (BCorDE).