Regular Expressions Anchors
Regular expressions: Anchors / assertions
Anchors (assertions) allow us to fix where a match has to start or end
\A Match only at string start
^ Match only at string start (default)
Match only at a line start (in multi-line matching)
\Z Match only at string end modulo a preceding \n
\z Match only at string end
$ Match only at string end modulo a preceding \n
Match only at a line end (in multi-line matching)
\b Match word boundary (between \w and \W)
\B Match except at word boundary
Targ et stri ng " one two \ n three four \ nfive \n"
Do match Do not match
'/\Aone/' (at string start) '/^three/' (not a string start)
'/^three/m' (at line start) '/four\Z/' (not at string end mod \n)
'/four$/m' (at line end) '/five\z/' (not a string end)
'/five\Z/' (at string end mod \n)
'/\bone\b/' (a word in string) '/\bour\b/' (not a word in string)
COMP284 Scripting Languages Lecture 6 Slide L6 – 7