An anchor is a regular-expression construct that constrains a match to a position such as the beginning or end of a string.
A regular-expression anchor constrains a match to a position instead of matching ordinary characters. The ^ anchor targets the beginning of a string, while $ targets its end. They help prevent a pattern from matching only a valid-looking fragment inside extra text. section 21.2
Tightening an email pattern
The pattern \w{1}\.\w+@company\.com can match the valid-looking portion of Samuel.smith@company.com.org. Adding ^ and $ requires the entire string to fit the pattern: ^\w{1}\.\w+@company\.com$. section 21.2
How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it
How deep it goes
Defines beginning and ending anchors through an email example, but defers broader regular-expression syntax.
Examples
Demonstrates anchors by tightening a company email pattern that otherwise accepts trailing text.
What it assumes
Assumes basic regular-expression pattern use and introduces anchors as a way to control match boundaries.
What you can do after
Apply via ^ and $ anchors to require a complete-string match and avoid accepting extra surrounding text.
How it connects
Connects anchors with regular expressions, string matching, and email-address patterns.
The Well-Grounded Rubyist, Fourth EditionA regular-expression anchor is a zero-width pattern assertion that constrains a match to a structural position such as the start or end of text.
Python How-ToA regular-expression anchor is a zero-width matching construct that constrains a match to a position such as the beginning or end of text.
regular expressionpart of · A formal pattern notation that describes sets of strings and enables text to be matched, searched, extracted, or transformed.
word boundaryincludes · A word boundary is a position marking separation between words or a transition between word and non-word characters in text or a regular expression.
end anchorincludes · An end anchor is a zero-width regular-expression assertion that constrains a match to the end of the input or, in multiline mode, a line.
This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.