bman2013
bman2013

Reputation: 407

Using RegEx with Notepad ++ for Mass Replacements

I have an xml file, where the code below repeats a few hundred times. I need to remove all instances of where Block Name="ZG123CZ123". Instead of having the remove start on the line where the app name condition is met, it needs to start at <device> and end at </device> for complete removal of where app condition is met. How exactly would I go about in doing this using Notepad ++ RegEx replacements?

<Device DevID="14212111" Model="" GroupID="">
  <Block Name="DG" LastFileDld="" LastFullDld="">
    <Group ZD="3">
      <DSize ParamName="*UNZIP" Value="DG.ZIP"/>
      <DSize ParamName="*GO" Value="F:DG.OUT"/>
    </Group>
    <Group ZD="15">
      <DSize ParamName="#LOGPORT" Value="NONE"/>
      <DSize ParamName="CDH6" Value=""/>
    </Group>
  </Block>
  <DevFiles />
</Device>
<Device DevID="14212111" Model="" GroupID="">
  <Block Name="ZG123CZ123" LastFileDld="" LastFullDld="">
    <Group ZD="1">
      <DSize ParamName="KEY" Value="RAM"/>
      <DSize ParamName="REM" Value="***"/>
      <DSize ParamName="*UNZIP" Value="I:ZG123CZ123.ZIP"/>
    </Group>
    <Group ZD="15">
      <DSize ParamName="REM" Value="="***""/>
      <DSize ParamName="#DEBUGPORT" Value=""/>
    </Group>
  </Block>
  <DevFiles />
</Device>

Upvotes: 1

Views: 95

Answers (1)

Sam
Sam

Reputation: 20486

Use this:

(?s)<Device(?:(?!<\/Device).)*<Block[^>]+Name="ZG123CZ123".*?<\/Device>

Demo


Explanation:

  • (?s) is the single-line modifier, which makes . include newline characters.
  • <Device looks for the beginning of a Device element.
  • (?:(?!<\/Device).)* is essentially .*, but each time we try to match a character we make sure we aren't matching the end of a Device element (<\/Device) by using a negative lookahead. Note that you can't just make this a lazy match, since it would continue until if found the <Block Name="ZG123CZ123"> (that is the next chunk).
  • <Block[^>]+Name="ZG123CZ123" will look for the beginning of a Block element, followed by non > character(s), followed by the Name attribute equaling ZG123CZ123.
  • .*? is a lazy match to eat up the rest of the Device element.
  • <\/Device> we've made it to the end!

Upvotes: 1

Related Questions