0

In vim, I'm trying to match all lines that has ^A. but not followed by a B. on the next line.
I'm matching it with /\v^A\. .*$\n[^B]. This correctly identifies the relevant portion in question 2., but not in question 3. I'm not able to figure out why. The ending matches \n\n. And [^B] should match \n but it doesn't. What am I doing wrong?

1. Hello?
A. aaaaaaaa
B. bbbbbb
C. ccccccc
D. ddddd

2. Hello?
A. aaaaaaaa B. bbbbbb
C. ccccccc
D. ddddd


3. Hello?
A. aaaaaaaa B. bbbbbb C. ccccccc D. ddddd

4. Hello?
4
  • 1
    It's an empty line, so you probably must match ([^B]|$) Commented Jun 8, 2020 at 15:10
  • that seems to work. But doesn't [^B] also match the $?
    – sathishvj
    Commented Jun 8, 2020 at 20:09
  • no it doesn't. Otherwise your Hello line would also have to match ^Hello?[^a][^b][^c][^d] etc, does not make any sense Commented Jun 9, 2020 at 6:20
  • I don't get it. [^B] says any thing but capital B. And $ != B. Is that not valid?
    – sathishvj
    Commented Jun 9, 2020 at 6:50

1 Answer 1

1

As :help /[\n] documents, a collection like [^B] does not match an end-of-line character.

This makes it Vi compatible: Without the "_" or "\n" the collection does not match an end-of-line.

You have two options: Either explicitly include a newline, so that your line followed by an empty line also matches:

/\v^A\. .*$\n([^B]|$)/

Or do a negative lookahead match with /\@!:

/\v^A\. .*$\nB@!/
1
  • The things I don't know about vim!
    – sathishvj
    Commented Jun 11, 2020 at 7:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .