Anchors

Anchors are used to mark out a position that the regex parser should start or end matching from.

Example

Let’s say we wanted to match the words ‘hat’ or ‘cape’ in the text below.

He took off his hat and cape and sat down wearily. His nemesis had escaped him again.

If we created a simple regex to match either of these words such as:

cape|hat

we’d get the matches below.

He took off his hat and cape and sat down wearily. His nemesis had escaped him again.

The first two matches are fine. The third is unlikely to be a match we wanted.

To fix this we need to consider that our matches should be complete words rather than parts of words.

A complete word will normally (unless it’s at the beginning of the text, the end of the text or both) have something which isn’t a ‘word’ type character on either side of it. That something could be whitespace, a comma or some other  punctuation; generally anything other than another letter.

So for our regex parser to start matching from the start of a word, it needs to know where the boundaries between the word start and end exist. We do this with an anchor; specifically a ‘Word Boundary’ anchor.

Our full regex will be

\bcape|hat\b

which breaks down like this:

\b boundary (at the start of the word)
cape match the word cape
| alternation symbol
hat match the word hat
\b boundary (at the end of the word)

More about Anchors

Boundary anchors are just one type of anchor. Other examples include: start of string, end of string, end of previous match.

An alternative to anchors are Lookarounds. Lookarounds allow for complete regex to be used to establish anchor points.

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 Anchors are in Textpression check out the video.