Anchoring The Middle of The Regex Match to The Start of a Line or a Word

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.

  1. Click the New Formula button on the top toolbar to clear out all settings on the Samples, Match, and Action panels.
  2. On the Samples panel, paste in one new sample:
    begin content end
    begin something else end
    begin content bend
    beginning content end
    begin content ending
  3. Select the word “begin” at the very start of the sample text and click the Mark button.
  4. Adjacent to “begin”, select “ content ” including the space before and after the word and click the Mark button.
  5. Adjacent to that, select the word “end” and click the Mark button.
  6. On the Match panel we now have 3 literal text fields that each match one of the 3 pieces of text that we marked. For fields 1 and 3 this is correct. But we want field 2 to allow any text.
  7. Change field 2 to repeat between 1 and unlimited times.
  8. Set “how to repeat this field” to “as few times as possible” for field 2.
  9. In the “pattern to match field” drop-down list, select “match anything”.
  10. In the “match anything except” drop-down list, select “nothing” so the field can match truly anything.
  11. On the Regex panel, select “Python 3.13” as your application, turn off free-spacing, and turn off mode modifiers. Click the Generate button, and you’ll get this regular expression:
    begin.+?end
    Required options: Case sensitive; Dot doesn’t match line breaks.
    Unused options: Exact spacing; ^$ don’t match at line breaks.
  12. The Samples panel shows how the regex matches (part of) every line in the sample, because we did not add the anchors:
    begin content end
    begin something else end
    begin content bend
    beginning content end
    begin content ending
  13. On the Match panel, “begin regex match at” to “start of word”, and set “end regex match at” to “end of word”.
  14. Click on the colored rectangle for field 2 to make the field buttons work on that field.
  15. Click the Add Prior Field button Add Prior Field to insert a new field 2 before the existing one.
  16. Select “anchor the match” in the “kind of field” drop-down list for field 2.
  17. Select “end of a word” in the “anchor the match to” drop-down list.
  18. Click on the colored rectangle for field 3 to make the field buttons work on that field.
  19. Click the Add Next Field button Add Next Field to insert field 4 after field 3.
  20. Select “anchor the match” in the “kind of field” drop-down list for field 4.
  21. Select “start of a word” in the “anchor the match to” drop-down list.



  22. The Regex panel now generates our regular expression with added word boundaries:
    \bbegin\b.+?\bend\b
    Required options: Case sensitive; Dot doesn’t match line breaks.
    Unused options: Exact spacing; ^$ don’t match at line breaks.
  23. The Samples panel shows how the new regex matches only the first two lines, as only those have “begin” and “end” as proper words.
    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.
Unused options: Exact spacing; Dot doesn’t match line breaks; ^$ don’t match at line breaks; Numbered capture; Names must be unique; Greedy quantifiers; Allow zero-length matches.

Related Examples

Reference