当字匹配时,Notepad ++ RegEx在标签之间移除(Notepad++ RegEx remove between tags when word matched)

我有一个类似于数字的问题,这次我需要将它用于关键字。 下面是我从KML文件中使用的示例数据。 我想删除所有包含单词footway的地标。

<Placemark> <styleUrl>#nothing</styleUrl> <ExtendedData> <SchemaData> <SimpleData>highway</SimpleData> </SchemaData> </ExtendedData> <LineString> <coordinates>0.0000,0.0000,0</coordinates> </LineString> </Placemark> <Placemark> <styleUrl>#nothing</styleUrl> <ExtendedData> <SchemaData> <SimpleData>footway</SimpleData> </SchemaData> </ExtendedData> <LineString> <coordinates>0.0000,0.0000,0</coordinates> </LineString> </Placemark>

我尝试使用以下,但它捕获一切

(?i)<Placemark>.*?footway.*?</Placemark>

以下是我的记事本++设置

Find what: (?i)<Placemark>.*?footway.*?</Placemark> Replace with: Warp around Search Mode: Regular expression & mathces newline

I had a similiar question that was used for numbers this time I need to use it for keyword. Below is the sample data that I'm using from a KML file. I would like to remove all placemarks that contain the word footway.

<Placemark> <styleUrl>#nothing</styleUrl> <ExtendedData> <SchemaData> <SimpleData>highway</SimpleData> </SchemaData> </ExtendedData> <LineString> <coordinates>0.0000,0.0000,0</coordinates> </LineString> </Placemark> <Placemark> <styleUrl>#nothing</styleUrl> <ExtendedData> <SchemaData> <SimpleData>footway</SimpleData> </SchemaData> </ExtendedData> <LineString> <coordinates>0.0000,0.0000,0</coordinates> </LineString> </Placemark>

I tried to use the following however it is capturing everything

(?i)<Placemark>.*?footway.*?</Placemark>

Below is my notepad++ settings

Find what: (?i)<Placemark>.*?footway.*?</Placemark> Replace with: Warp around Search Mode: Regular expression & mathces newline

最满意答案

这是一个方法:

找到: <Placemark>(?:(?!<Placemark).)*footway(?:.(?!<Placemark))*</Placemark> 替换为: NOTHING

这将替换包含footway所有<Placemark>块,仅替换它们。

(?!<Placemark)是一个负向前瞻 ,假设在footway之前没有<Placemark> ,因此,当你有许多<Placemark> ,正则表达式一次匹配一个<Placemark> 。

(?:(?!<Placemark).)*是非捕获组,发生0次或更多次并且不包含(?!<Placemark)后跟一个字符。

Here is a way to go:

Find what: <Placemark>(?:(?!<Placemark).)*footway(?:.(?!<Placemark))*</Placemark> Replace with: NOTHING

This will replace all <Placemark> blocks that contain footway and only them.

(?!<Placemark) is a negative lookahead that assumes there're no <Placemark> before footway, so, when you have many <Placemark>'s the regex matches a single <Placemark> at a time.

(?:(?!<Placemark).)* is a non capture group, that occurs 0 or more times and does not contain (?!<Placemark) followed by a character.

更多推荐