Positive Look-behind


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

To demonstrate, lets re-implement our solution from the match-boundaries section using a pattern containing a positive look-behind assertion.

In this example we have the following buffer, which contains 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
/\(2022-\)\@<=\(1\d-\d\d\)

Our task is to match the month and day on all lines for which the year is 2022 and the month is in Q4 (months 10, 11, and 12).

The first step is to create the pattern that matches our desired content, the month and day:

\(1\d-\d\d\)

The next step is to define the test pattern. We only want to match lines for which the year is 2022, so we can use the test pattern:

\(2022-\)

Finally, to mark the test pattern as a positive look-behind assertion we add \@<=, which gives us the final pattern:

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

When we execute this we get the same result as before.

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:6