Precedence
Because of the dearth of operators for regular expressions, precedence is easy to remember, and what one would logically expect. The following list itemizes the operators from highest to lowest precedence:
Quantifiers ('?', '*', '+', and '{n,m}')
Concatenation
Alternatives ('|')
Of course, grouping with '(' and ')' overrides precedence, as in almost any programming language.
For clarity, here are a few precedence examples:
Regular Expression |
Description |
Matching Strings |
X|abc? |
Either the character 'X' or the sequence 'ab' followed by an optional 'c'. |
"X" "ab" "abc" |
Ab+c|de+f |
The sequence 'abc' (with one or more 'b') or the sequence 'def' with one or more 'e'). |
"abbbc" "deeef" |
(X|a?)bc |
'Xbc' or 'abc', 'a' being optional on the latter string. |
"Xbc" "abc" "bc" |
ab|c{1,2}|de |
'ab', or 1 or 2 'c', or 'de'. |
"ab" "c" "cc" "de" |
A(xyz)? |
'a' followed by zero or one 'xyz'. |
"a" "axyz" |