Next: , Previous: , Up: Common Operators   [Contents][Index]


18.3.5 The Alternation Operator (| or \|)

If the syntax bit RE_LIMITED_OPS is set, then Regex doesn’t recognize this operator. Otherwise, if the syntax bit RE_NO_BK_VBAR is set, then ‘|’ represents this operator; otherwise, ‘\|’ does.

Alternatives match one of a choice of regular expressions: if you put the character(s) representing the alternation operator between any two regular expressions a and b, the result matches the union of the strings that a and b match. For example, supposing that ‘|’ is the alternation operator, then ‘foo|bar|quux’ would match any of ‘foo’, ‘bar’ or ‘quux’.

The alternation operator operates on the largest possible surrounding regular expressions. (Put another way, it has the lowest precedence of any regular expression operator.) Thus, the only way you can delimit its arguments is to use grouping. For example, if ‘(’ and ‘)’ are the open and close-group operators, then ‘fo(o|b)ar’ would match either ‘fooar’ or ‘fobar’. (‘foo|bar’ would match ‘foo’ or ‘bar’.)

The matcher usually tries all combinations of alternatives so as to match the longest possible string. For example, when matching ‘(fooq|foo)*(qbarquux|bar)’ against ‘fooqbarquux’, it cannot take, say, the first (“depth-first”) combination it could match, since then it would be content to match just ‘fooqbar’.

Note that since the default behavior is to return the leftmost longest match, when more than one of a series of alternatives matches the actual match will be the longest matching alternative, not necessarily the first in the list.


Next: List Operators ([] and [^]), Previous: Repetition Operators, Up: Common Operators   [Contents][Index]