What other special characters are anchors?

QuestionsQuestions8 SkillsProDec, 18 2025
0112

In addition to ^, which anchors to the start of a line, the other primary anchor used in regular expressions is:

1. $ (The End-of-Line Anchor)

While ^ matches the beginning, $ matches the end of a line.

  • Example: nl -b p'password$' file.txt
    • This would only number lines that end exactly with the word "password".

Other Common Anchors

Depending on the tool (like grep or vim) or the regex "flavor" you are using, you might also encounter these:

2. \< and \> (Word Boundaries)

These are used to anchor to the start or end of a specific word, rather than the whole line.

  • \<host: Matches "host", "hostname", or "hostess", but only if "host" is at the start of the word.
  • host\>: Matches "host" or "ghost", but only if "host" is at the end of the word.

3. \b (Universal Word Boundary)

In many modern regex engines (like those used in Python or JavaScript), \b acts as an anchor for the boundary between a word character (like a letter) and a non-word character (like a space or punctuation).

  • \bcat\b: Matches the word "cat" exactly, but will not match "category" or "bobcat".

Quick Cheat Sheet:

Character Meaning Position
^ Start of line ^text
$ End of line text$
\< Start of word \<word
\> End of word word\>
\b Any word boundary \bword\b

In the context of the nl command and the pattern ^[^#], combining the ^ anchor with brackets is one of the most powerful ways to filter which lines get numbered

0 Comments

no data
Be the first to share your comment!