Alternation

Alternation instructs the regex parser to attempt to match one of a number of alternatives.

example

Let’s say we want to match any one of  ’cat, ‘dog’ or ‘mouse’ in some text.

Our regex would simply be cat|dog|mouse

Which breaks down like this:

cat first alternative
| alternation symbol
dog second alternative
| alternation symbol
mouse third alternative

The parser will try to match ‘cat’. If that fails, it will backtrack to the position it was at when it tried to start matching ‘cat’ and then try again with ‘dog’ and so until a match is found.

Some sample matches are

The cat sat on the mat

The dog barked

The mouse sqeaked

More about alternation

Alternatives:

Are not restricted to containing plain text. They can contain full regex’s as well;

Can be grouped. This wasn’t necessary in our example above. However, if the alternation is part of a larger regex, then grouping will delimit where the alternation starts and ends.

Textpression

Here are some of the ways you could view the completed regex in Textpression.

You can paste plain text regex straight into Textpression and get an immediate visualisation as above.

Or you can create regex yourself using the simple drag and drop editor.

If you’d like to see how easy alternation is in Textpression check out the video.