DavidSzczesniak
12/20/2017 - 10:02 AM

Regex Anchors

These force the regex engine to start or end a match at a certain, fixed postion.

# The start of string anchor (\A) dictates that a match must start at the beginning of the string:
# also matches "lammed", "lawmaker" and "layman"
my $seven_down = qr/\Al${letters_only}{2}m/; # (letter 'l' and 'm')

# The end of line string anchor (\z) requires that a match ends at the end of the string:
# also matches "loom", still an obvious improvement
my $seven_down = qr/\Al${letters_only}{2}m\z/; 

\!h # There are also the ^ and $ assertions used to match the start and end of strings
# ^ does mean the start of the string, but in certain circumstances it can match the invisible point after the newline within the string

# Example:
# You want to find strings that have 'barney' at the absolute beginning of the string or anywhere after a newline.
/^barney/m

\!h # Similarly, $ does mean the end of the string, 
# but it can match the invisible point before a newline in the middle of the string

# Example:
# You want to find strings that have 'fred' at the end of any line, not just at the end of the entire string.
/fred$/m

\!h # Without the '/m', ^ and $ act just like \A and \Z. Its better to use \A and \Z unless you specifically want multiline matching.

\!h # Word Anchors

# The word-boundary anchor, \b, matching either end of the word.
# For example, /\bfred\b/ will match the word 'fred', but not ones like 'frederick', 'alfred', or 'manfred'.

# This is useful to ensure you dont find 'cat' in 'delicatessen', or 'fish' in 'selfishness'.
# You would normally use one word-boundary anchor, when using /\bhunt\ to match words like 'hunt' or 'hunting', but not 'shunt'.
# Or when using /stone\b/ to match words like 'sandstone' or 'flintstone', but not 'capstones'.

# The nonword-boundary anchor, \B, matching any point \b wouldn't match. 
# For example, the pattern /\bsearch\B/ will match 'searches' or 'searching', but not 'search' or 'researching'.