"Perl for Perl Newbies" - Part 2Regular ExpressionsAlternation with "|"

7.5. Alternation with "|"

One can specify several possible matching for an expression to match with |. For instance, dog|cat|mouse will match either "dog" or "cat" or "mouse". But naturally the options can themselves be regular expression which are as complex as perl supports.

The alternation operator will try to match as much of the expression as it can, so it is recommended to use grouping to bound it. (e.g: (?:dog|cat|mouse))

The following pattern matches a sequence of whitespace-delimited "words" that each may contain either a set of letters or a set of digits (= a positive integer):

(?:[a-zA-Z]+|[0-9]+)(?: +(?:[a-zA-Z]+|[0-9]+))*

Written by Shlomi Fish