简体   繁体   中英

Notepad++ Removing text between two strings if third string is present between other strings

I would like to remove the text between two strings, including those strings, if and only if a third string is present between those two strings. I would greatly prefer it if I could use the replace functionality in Notepad++ to do this.

Here's a mock-up of what I need altered:

asdfnjaslfjsa
asdfjaskldfsafkldj
asdfjsadfk
STRING_1
sanjvnlamf
 fas g
gsegvrs 
STRING_2
STRING_1
asf sf gfsjasak
qweuwiouqnv
STRING_3
awi iavbfa c
anfiab
STRING_2
STRING_1
asmorancm
anib fas
STRING_2
sdabfashbdfbc  ds

Changed to this:

asdfnjaslfjsa
asdfjaskldfsafkldj
asdfjsadfk
STRING_1
sanjvnlamf
 fas g
gsegvrs 
STRING_2
STRING_1
asmorancm
anib fas
STRING_2
sdabfashbdfbc  ds

What you are looking for can be achieved with the following construct:

STRING_1
(?:(?!STRING_2)[\s\S])*?
STRING_3
(?:(?!STRING_2)[\s\S])*?
STRING_2

This matches STRING_1 to STRING_2 only if STRING_3 is present in between (the technique is called a tempered greedy token ).


You can put it in one line as well:

 STRING_1(?:(?!STRING_2)[\\s\\S])*?STRING_3(?:(?!STRING_2)[\\s\\S])*?STRING_2 

See a demo on regex101.com .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM