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, while the rest of the pattern is free to drift with the tide.)
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, substr isn’t sufficient—or at least not
sufficiently convenient, compared to patterns.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access