1

I have a text file with lines like the following:

3005,23,1,NI,2348,2
3005,17,NI,2348
3005,23,1,NI,2348,2
3005,17,NI,2347

The 2348 and 2347 in those lines could actually be any numbers.

I want to find the lines starting with 3005,23 that are followed by a line starting with 3005,17 where the 5th value on the first line matches the 4th value of the second line (for example, 2348 in the first 2 lines of the example). I know how to do that using a capture group:

/3005,23,[0-9],[A-Z][A-Z],\([0-9]\+\),.*\n3005,17,[A-Z][A-Z],\1

That search will match the first 2 lines in the example above.

But how do I search for the lines starting with 3005,23 that are followed by a line starting with 3005,17 where the 5th value on the first line does not match the 4th value on the second line, so that it matches the last 2 lines in the example above?

1 Answer 1

1

I would do:

/3005,23,[0-9],[A-Z][A-Z],\([0-9]\+\),.*\n3005,17,[A-Z][A-Z],\1\@![0-9]\+

\@! match with zero width only if the preceding atom do not match.

More information width :help /\@!

Your Answer

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

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