In a regular expression, an anchor matches at a particular position rather than matching particular text. The Match panel has settings to force the regex match to start and/or end at the start of the text, a line, or a word. These add anchors to the start and end of the regular expression.
Matching at the start or end of a line or a word can also be useful in the middle of a regular expression. Suppose you want to match the words “begin” and “end” and any text between them. But you want “begin” and “end” to be whole words, not parts of longer words. You don’t want to match text between “beginning” and “bend”, for example. The steps below show how you can achieve this with RegexMagic. You can find this example as “Fields: anchor” in the RegexMagic library.
begin content end begin something else end begin content bend beginning content end begin content ending
and
this is correct. But we want field
to allow any text.
to repeat between 1 and unlimited times.
.begin.+?endRequired options: Case sensitive; Dot doesn’t match line breaks.
begin content end begin something else end begin content bend beginning content end begin content ending
to make the field buttons work on that field.
to insert a new field
before the existing one.
.
to make the field buttons work on that field.
to insert field
after field
.
.
\bbegin\b.+?\bend\bRequired options: Case sensitive; Dot doesn’t match line breaks.
begin content end begin something else end begin content bend beginning content end begin content ending
Most regular expression flavors, including Python, only have one type of word boundary that can match at either side of a word. For such applications, RegexMagic simply uses the word boundary that is available. In this case, that works just fine because a such word boundary at the start or end of a literal word in a regex can only match at the start or end of a word. RegexMagic does not verify whether that is the case though.
Some regular expression flavors, such as PCRE, support dedicated start-of-word and end-of-word anchors. For those applications, RegexMagic always uses the specific anchor that you selected. The Regex panel shows this regex if you change the application to PCRE2:
[[:<:]]begin[[:>:]]\N+?[[:<:]]end[[:>:]]Required options: Case sensitive.