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?
  • 1
    It's an empty line, so you probably must match ([^B]|$) – Christian Brabandt yesterday
  • that seems to work. But doesn't [^B] also match the $? – sathishvj yesterday
  • no it doesn't. Otherwise your Hello line would also have to match ^Hello?[^a][^b][^c][^d] etc, does not make any sense – Christian Brabandt yesterday
  • I don't get it. [^B] says any thing but capital B. And $ != B. Is that not valid? – sathishvj yesterday
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@!/
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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