Positions
Some regex constructs represent positions in the string to be matched, which is a location just to the left or right of a real character. These metasymbols are examples of zero-width assertions because they do not correspond to actual characters in the string. We often just call them "assertions". (They're also known as "anchors" because they tie some part of the pattern to a particular position.)
You can always manipulate positions in a string without
using patterns. The built-in substr
function lets
you extract and assign to substrings, measured from the beginning of
the string, the end of the string, or from a particular numeric
offset. This might be all you need if you were working with
fixed-length records, for instance. Patterns are only necessary when a
numeric offset isn't sufficient. But most of the time, offsets aren't
sufficient--at least, not sufficiently convenient, compared to
patterns.
Beginnings: The \A and ^ Assertions
The \A
assertion matches only at
the beginning of the string, no matter what. However, the
^
assertion is the traditional beginning-of-line
assertion as well as a beginning-of-string assertion. Therefore, if
the pattern uses the /m
modifier[8] and the string has embedded newlines,
^
also matches anywhere inside the string
immediately following a newline character:
/\Abar/ # Matches "bar" and "barstool" /^bar/ # Matches "bar" and "barstool" /^bar/m # Matches "bar" and "barstool" and "sand\nbar"
Used in conjunction with /g
, the
/m
modifier ...
Get Programming Perl, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.