Reputation: 343
I'm using vim to edit a document - an excerpt of which is:
--
aaa bbb
ccc ddd
eee fff
--
ggg hhh
iii jjj
kkk lll
--
I'd like to select all lines that come after (both) lines with "--" and new lines. In this example, I'd like to select "aaa bbb" and "ggg hhh". Is line-level "look behinds" possible in regex?
Upvotes: 2
Views: 780
Reputation: 40947
Vim has a nifty feature that you can take advantage of here. The following command will match the two lines requested (using search as an example):
/--\n\n\zs.*
The explanation:
/
- begin the search--\n\n
- the predicate as I understand the question\zs
- tells vim to begin the actual match here and throw away everything before this.*
- any character.The \zs
token allows you to tell vim where the pattern matching actually begins even though the regex as a whole must match. There is a complementary token \ze
which tells vim where to end the match.
While I've used the search function to show off this feature, these tokens can be used anywhere you can use a regex in vim.
For more information about this feature take a look at :help /\zs
and :help /\ze
.
Upvotes: 6
Reputation: 977
If you have only this kind of data like you provided you could use the lookbehinds with newline characters. Basically matching the line after "--" and a empty line. And answering "is line level lookbehind possible": Yes, a line break is nothing but a character denoted with \n
.
(?<=--\n\n).*
https://regex101.com/r/q5VESm/1
Upvotes: 0