Alternation
Alternation allows matching one of several phrases. This is more powerful than character classes, which are limited to characters.
Delimit the set of phrases with pipes—|
.
One of foo
, bar
, and baz
If only a part of the regex is to be “alternated”, wrap that part with a group—capturing or non-capturing.
Try
followed by one of foo
, bar
, and baz
Matching numbers between 100 and 250:
This can be generalized to match arbitrary number ranges!
Examples
Hex colours
Let’s improve one of our older examples to also match shorthand hex colours.
It is important that [0-9A-F]{6}
comes before [0-9A-F]{3}
. Else:
TipRegex engines try alternatives from the left to the right.
Roman numerals