1

First, I am new to regular expressions.

I have an XML File that is formatted as so:

<SCHED_TABLE LAST_UPLOAD="" ... TABLE_NAME="">
<JOB
  APPLICATION=""
  ...
  NODEID="foo"
  ...
>
</JOB>
<JOB
  APPLICATION=""
  ...
  NODEID="bar"
  ...
>
</JOB>
</SCHED_TABLE>

With a whole lot of lines in between. What I need to do is write a regular expression to find all the JOB's where NODEID != foo. Those that do NOT equal foo will be replaced with a blank, therefore deleting those jobs. The whole job needs to be deleted including the open and close JOB tag.

Any advice for this?

1 Answer 1

3

Try this (untested):

Find:

<JOB[^>]+?NODEID="(?!foo)[^>]+?>.+?</JOB>

Replace with blank.

Make sure . matches newline (or whatever that option is called -- I'm away from my normal computer) is checked.

Breakdown of how this works:

  • <JOB matches the start of a JOB tag.
  • [^>]+? matches everything that is not a > symbol, but the ? means "don't be greedy" -- that is, don't use more characters than you need to.
  • NODEID=" means match literally those characters.
  • (?!foo) is a negative look-ahead pattern. It means, "This is not a match if everything has worked so far but the text following this point is foo."
  • [^>]+?, again, all non-> characters, but not greedy.
  • > match the > character exactly.
  • .+? Match any string of characters, but don't be greedy (i.e., stop when you hit the last part of the regex, </JOB>)
  • </JOB> closing tag.

Ordinarily, .+? would not match newline characters, which is why you need to tell Notepad++ to let it do so.

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

2 Comments

Ed, if you don't mind, can you take a moment and explain each step of this expression in regular terms? I want to make sure I understand exactly what is going on. Again, thanks for the quick answer!
Glad it helped. I added an explanation above.

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.