.*[aAeE]ffect.*
Sometimes you wish to match one string OR another.
Use the disjunction operator |
for this.
The disjunction operator has low precidence, so
that the complete subexpression on the left or the complete subexpression
on the right is matched.
To indicate a disjunction between three or more subexpressions, separate each pattern with the disjunction operator.
Regular Expression | Matches | Does Not Match |
---|---|---|
cat|dog | "cat" or "dog" | "catdog" |
cat|dog|rat | "cat" or "dog" or "rat" | "cadorat" |
color|colour | "color" or "colour" | "colorolour" |
a*|b* | any number of 'a' or any number of 'b' | "aaabbbbb" |
[abc]+[123]*|[def]*[456]+ | "aa223" or "bb" or "dee5" or "544" | "aa444" |
Detail: The Kleene star has high precidence, so it applies to just one character on its left. Disjunction has low precidence, so it applies to entire subexpressions on its left and right.
So cat*|dog*
means cat*
OR dog*
.
The subexpression cat*
means "ca" followed by zero or more characters 't'.
The subexpression dog*
means "do" followed by zero or more characters 'g'.
Regular Expression | Matches | Does Not Match |
---|---|---|
cat*|dog* |
"cat" "ca" "cattt" "do" "dogggg" |
"cado" "catdog" "catcatcat" "dogdogdog" |
If you want to match catdogcat or dogdogcat and similar, use parentheses: (cat|dog)*
.
See details in a few pages.
What is another way to match either color or colour ?