2

can you tell me the what to search for in notepad++ in order to find all nodes with the optioncode 09 below and delete that node? For example, I would like to be able to search on the below xml and delete the first entry and be left with is below that.

I tried to searching for <Vehicle>.*?</Vehicle> which works to replace a blank value, however I want to also add criteria to search for the 09 value in the entire node. Is it possible to add a search condition for the ">09<" text string value?

Search here:

  <Vehicle>
    <InvoiceDateTime>2016-03-20T00:00:00</InvoiceDateTime>
    <InvoiceChargeCents>63</InvoiceChargeCents>
    <OptionCode>09</OptionCode>
    <JobEndDateTime>2016-03-19T00:00:00</JobEndDateTime>
    <AuthorizationCode />
  </Vehicle>
  <Vehicle>
    <InvoiceDateTime>2016-03-20T00:00:00</InvoiceDateTime>
    <InvoiceChargeCents>63</InvoiceChargeCents>
    <OptionCode>35</OptionCode>
    <JobEndDateTime>2016-03-19T00:00:00</JobEndDateTime>
    <AuthorizationCode />
  </Vehicle>

Return the entry below:

  <Vehicle>
    <InvoiceDateTime>2016-03-20T00:00:00</InvoiceDateTime>
    <InvoiceChargeCents>63</InvoiceChargeCents>
    <OptionCode>35</OptionCode>
    <JobEndDateTime>2016-03-19T00:00:00</JobEndDateTime>
    <AuthorizationCode />
  </Vehicle>

1 Answer 1

2

My approach consists in matching the <Vehicle> opening node, and then restrict dot matching so that it could match neither opening nor closing Vehicle nodes with a tempered greedy token:

<Vehicle>(?:(?!</?Vehicle>).)*>09<.*?</Vehicle>\R*

The . matches newline option should be enabled.

See the regex demo, replace with an empty string.

The (?:(?!</?Vehicle>).)* is a tempered greedy token that matches any text up to the first >09< after the closest <Vehicle> opening node.

enter image description here

Note that \R* matches zero or more newline sequences (either CRLF, or CR, or LF).

Also please note that a more efficient pattern will be the unrolled version of the above pattern:

<Vehicle>[^<]*(?:<(?!\/?Vehicle>)[^<]*)*>09<.*?<\/Vehicle>\R*
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

See another regex demo

It matches the same text, but is more efficient with larger texts.

Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, looks like this works perfectly, even when there are multiple blocks without the >09< above. Thanks for you help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.