Alternation
The alternation (OR) character in a regular expression is a pipe (|
). This is used to combine several possible regular expressions. A simple example is to match a list of words:
'one', 'two', 'three' -match 'one|three'
The alternation character has the lowest precedence; in the previous expression, every value is first tested against the expression to the left of the pipe and then against the expression to the right of the pipe.
The goal of the following expression is to extract strings that only contain the words one
or three
. Adding the start- and end-of-string anchors ensures that there is a boundary. However, because the left and right are treated as separate expressions, the result might not be as expected when using the following expression:
PS> 'one', 'one hundred', 'three', 'eighty three' -match '^one|three$'
one
one hundred
three
eighty three
The...