Assertions
Also called anchors or even zero-width assertions, these specify a position in the source text, not any text itself.
-
^- match the beginning of the source text. -
$- match the end of the source text. -
\b- match a boundary between word and non-word characters.
Drill¶
- Use the pattern
^Re.- Now enable the
mfor multiline option.- Can you match a literal
.at the end of a line? Try it with and without themoption.- Use the pattern
theand look closely at all the matches found.- Now try
\bthe.the\b.\bthe\b.
Practice Exercise¶
When the
mormultilineoption is used,
^matches the absolute beginning of the source text, or any position immediately after a newline.
$matches the very end of the source text, or any position immediately before a newline.
\Amatches the absolute beginning of the source text.
\Zmatches the very end of the source text.