3

EDIT: Don't waste time reading this question unless you're interested in a good example of why you should always check the :help first: a severe case of PEBKAC.

Consider the text:

foo
.
.
barbaz

Enter the following search:

/\(foo\_.\{-}bar\)baz
 \(             \)     # a group, containing
   foo                 #   'foo', followed by
         \{-}          #   as few as possible instances of
      \_.              #   anything, including newlines, followed by
             bar       #   'bar'
                  baz  # followed by 'baz'

It matches!

Now let's insert a positive lookbehind, so we only match the baz:

/\(foo\_.\{-}bar\)\@<=baz
                  \@<=     # positive lookbehind

E486: Pattern not found: \(foo\_.\{-}bar\)\@<=baz

What's going on?

N.B. To check I'm using the zero-width @<= correctly, I tried removing the multiline part of the regex:

/\(bar\)\@<=baz

This matches the baz, as expected.

Note also that I tried manually specifying both of Vim's regular expression engines, which didn't help.

Finally, note that using \zs in place of \@<= works here, but not in the context in which I actually want to use the expression.

Here's another, arguably even simpler, but even more confusing example:

foo
foo
barbaz

This matches:

/\(foo\nbar\)baz

This matches:

/\(foo\nbar\)\@<=baz

This doesn't match:

/\(foo\nfoo\nbar\)\@<=baz

Am I doing something wrong, is this a documented limitation of Vim's regular expressions, or is it just a bug?

1 Answer 1

4

Why spend 2 minutes reading :help /\@<= when you could spend 20 writing an exhaustive question for vi.se?

Theoretically these matches could start anywhere before this position. But to limit the time needed, only the line where what follows matches is searched, and one line before that (if there is one). This should be sufficient to match most things and not be too slow.

(emphasis mine)

Probably I should try actually reading the documentation before asking if something is documented.

As always, je suis le idiot. :endless-facepalm-emoji:

5
  • 1
    Don't be so hard on yourself. ;)
    – B Layer
    Commented Feb 25, 2021 at 11:52
  • 1
    @BLayer It's mostly for effect :) (...mostly). If I was really upset with myself I'd have left it deleted instead of undeleting it and self-answering.
    – Rich
    Commented Feb 25, 2021 at 11:59
  • 1
    I figured...but even so you were pretty rough on yourself so I had to say something . :D
    – B Layer
    Commented Feb 25, 2021 at 12:09
  • 1
    @BLayer It's appreciated, either way!
    – Rich
    Commented Feb 25, 2021 at 12:17
  • 1
    My pleasure. Good find +1, by the way (even if it's right there in help). I tried matching the same thing with PCRE equivalent and it worked so I thought, too, that you might have found a bug. I didn't know...or, more likely, I forgot after reading it a long time ago...that that restriction was in place.
    – B Layer
    Commented Feb 25, 2021 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.