cfRegeX


Alternation

When your regex has multiple alternative paths that can be matched, you can use alternation to enable each of the paths to be tried as required. This is done by providing a pipe "|" delimited list of options, such as "this|that|other".

Since generally you only want part of a regex to have different paths, alternation is usually done inside either groups or lookarounds, both of which limit the scope of the alternatives to within their paretheses.

It is important to remember that the regex engine will not try each alternative if it does not need to - it will try the first, and see if the rest of the pattern matches. If it does not, the regex engine will backtrack to the start of the sub-expression and try the next alternative.

A common mistake is to try and use the "|" inside a character class, this is not necessary, and will simply allow the pipe character to be matched.