Chapter 4. Look Around
Until this point, we have learned different mechanisms of matching characters while discarding them. A character that is already matched cannot be compared again, and the only way to match any upcoming character is by discarding it.
The exceptions to this are a number of metacharacters we have studied, the so-called zero-width
assertions. These characters indicate positions rather than actual content. For instance, the caret symbol (^
) is a representation of the beginning of a line or the dollar sign ($
) for the end of a line. They just ensure that the position in the input is correct without actually consuming or matching any character.
A more powerful kind of zero-width assertion is look around, a mechanism with which it is possible to match a certain previous (look behind) or ulterior (look ahead) value to the current position. They effectively do assertion without consuming characters; they just return a positive or negative result of the match.
The look around mechanism...