Alternation with "|"

Contents | Up | Prev | Next

# 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 sentence that may contain numbers:

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

Contents | Up | Prev | Next