Lookarounds often cause confusion to the regex apprentice. I believe this confusion promptly disappears if one simple point is firmly grasped. It is that at the end of a lookahead or a lookbehind, the regex engine hasn't moved on the string. You can chain three more lookaheads after the first, and the regex engine still won't move. In fact, that's a useful technique.
A quick syntax reminder
This page digs deep into the details of lookahead and lookbehind and assumes you've already become familiar with the basic syntax, perhaps by reading the lookaround section of the reference on (? … ) syntax. As a quick reminder before we dive in, here are the four lookarounds.
Lookaround | Name | What it Does |
---|---|---|
(?=foo) | Lookahead | Asserts that what immediately follows the current position in the string is foo |
(?<=foo) | Lookbehind | Asserts that what immediately precedes the current position in the string is foo |
(?!foo) | Negative Lookahead | Asserts that what immediately follows the current position in the string is not foo |
(?<!foo) | Negative Lookbehind | Asserts that what immediately precedes the current position in the string is not foo |
Read full article from Lookahead and Lookbehind Tutorial—Tips &Tricks
No comments:
Post a Comment