SydneyFife
SydneyFife

Reputation: 23

regex in Notepad++: remove any number of lines between two given expressions

Input file:

[keyword1]
text0
"text1" = "text2"

[text3 keyword1 text4]
[text5]
[keyword2 text6]
"text7" = "text8"
"text9"
"text10" = "text11"

[text12]
"text13" = "text14"
text15

I want to match each 'entry' in full, if it contains a keyword in its first line. What I mean by 'entry in full' is: the first line, which is always in rectangular brackets, and any number of lines below it, basically until we hit the next [ character.

I know how to do it if I knew beforehand how many lines there would be in each 'entry', e.g. for just one line below I'd do something like this:

^\[(.+)(keyword1|keyword2)(.+)\]\r\n(.+)\r\n

But I don't know how to do it with an arbitrary number of lines. I have experimented a bit with the ". matches newline" feature of NP++, but all that got me so far was that the expressions will greedily match the whole file to the end.

Help?

//edit: so, the desired output based on the above example would be (maintaining original line numbers for clarity):

[text5]
[text12]
"text13" = "text14"
text15

//edit#2: cleared up the example input / desired output so that it's clear they are not meant literally. Q has already been answered though :)

Upvotes: 2

Views: 150

Answers (2)

Sunny Patel
Sunny Patel

Reputation: 8077

You can use the following pattern (with . matches newline checked) and replace with nothing:

\[[^\]]*?(with keyword)[^\]]*?\][^\[]*

Replace in the parenthesis with all the keywords separated by pipes |.

Basically searches for the pattern with square brackets and searching until keyword is found inside the brackets without checking the next line.

Since the . matches newline option is checked, we want to be careful when to greedily match.

Demo in action

enter image description here

Upvotes: 0

emsimpson92
emsimpson92

Reputation: 1778

This pattern should cover all your bases: \n^.*[^][\n]+$

Here's a demo

\n removes any new line characters before the line

^.* is designed to catch the "line x:" part

[^][\n]+$ matches one or more characters that aren't new lines, or square brackets up until the end of the line.

Upvotes: 1

Related Questions