Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
899 views
in Technique[技术] by (71.8m points)

vim: find a line containing pattern that is not in the neighbouring lines - Super User


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You'd want to use regex capture groups, as follows:

/v%(PATTERN.*
.*)@<!(PATTERN)%(.*
.*1)@!

Example

This might look scary, but it's actually quite simple:

  1. v is used to enable "very magic" regex (avoids the usage of backslashes for most special characters)
  2. PATTERN.* .* is PATTERN, followed by anything, a newline, and anything again
  3. (...)@<! is the pattern for negative look-behind: it means that we don't want what's between the braces to exist. So in our case, all of step 2 shouldn't exist
  4. (PATTERN) This is our actual word, this should exist! We capture it with braces for future usage
  5. .* .*1 is anything, a newline, anything again, and our captured group (see step 4)
  6. %(...)@! is a negative look-ahead, as before, we don't want what's between the braces to exist.

So basically, we want PATTERN, which is:

  • not preceded by a line containing PATTERN
  • not succeeded by a line containing PATTERN

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...