Positive Look-ahead


Positive Look-Ahead patterns are look-around patterns that match only if the text immediately to the right of the match also matches the test-pattern. As with all look-around patterns, the "test pattern" is not part of the match.

This can be better explained with an example. Using our buffer containing a variety of dates:

Initial Conditions
2022-01-14
2022-07-09
2022-10-23
2022-12-20
2023-01-26
2023-03-17
2023-08-06
2023-11-05
COMMANDTop1:1
/\(\d\d\d\d\)\(-1\)\@=

suppose we want to match all years for which the month is in Q4 (months 10, 11, and 12). The first step is to create the pattern that matches our desired content, the year portion of the date:

\(\d\d\d\d\)

Now that we have defined the main pattern, the next step is to define the test pattern. We only want to match the year when the month starts with a "1", so we can use the test pattern:

\(-1\)

Finally, to mark the test as a positive look-ahead assertion we use \@= to get the final pattern:

\(\d\d\d\d\)\(-1d\)\@=

when we execute this pattern:

Move cursor down
2022-01-14
2022-07-09
2022-10-23
2022-12-20
2023-01-26
2023-03-17
2023-08-06
2023-11-05
NORMAL33%3:1
 

Note that although our main pattern will match any year, only those years for which the month also matched the test pattern are highlighted.