- publishing free software manuals
Perl Language Reference Manual
by Larry Wall and others
Paperback (6"x9"), 724 pages
ISBN 9781906966027
RRP £29.95 ($39.95)

Sales of this book support The Perl Foundation! Get a printed copy>>>

12.7 Assertions

Assertions are conditions that have to be true; they don't actually match parts of the substring. There are six assertions that are written as backslash sequences.

\A
\A only matches at the beginning of the string. If the /m modifier isn't used, then /\A/ is equivalent with /^/. However, if the /m modifier is used, then /^/ matches internal newlines, but the meaning of /\A/ isn't changed by the /m modifier. \A matches at the beginning of the string regardless whether the /m modifier is used.
\z, \Z
\z and \Z match at the end of the string. If the /m modifier isn't used, then /\Z/ is equivalent with /$/, that is, it matches at the end of the string, or before the newline at the end of the string. If the /m modifier is used, then /$/ matches at internal newlines, but the meaning of /\Z/ isn't changed by the /m modifier. \Z matches at the end of the string (or just before a trailing newline) regardless whether the /m modifier is used. \z is just like \Z, except that it will not match before a trailing newline. \z will only match at the end of the string - regardless of the modifiers used, and not before a newline.
\G
\G is usually only used in combination with the /g modifier. If the /g modifier is used (and the match is done in scalar context), Perl will remember where in the source string the last match ended, and the next time, it will start the match from where it ended the previous time. \G matches the point where the previous match ended, or the beginning of the string if there was no previous match. Mnemonic: Global.
\b, \B
\b matches at any place between a word and a non-word character; \B matches at any place between characters where \b doesn't match. \b and \B assume there's a non-word character before the beginning and after the end of the source string; so \b will match at the beginning (or end) of the source string if the source string begins (or ends) with a word character. Otherwise, \B will match. Mnemonic: boundary.
ISBN 9781906966027Perl Language Reference ManualSee the print edition