The GRAPKE
The GRAPKE

Reputation: 843

regex to match xml tags not containing other tags

Let's say I want to look for XML like this:

<Address>
    <Street>Windsor</Street>
</Address>

and I do not want to match XML like this:

<Address>
    <Number>15</Number>
    <Street>Windsor</Street>
</Address>

That is, I am looking for addresses where the Address node does not contain a Number tag.

I tried patterns like

<Address>(?!Number)</Address>

or

<Address>.*?(?!Number).*?</Address>

But I can't quite figure it out :-(

Any ideas?

Upvotes: 2

Views: 1499

Answers (1)

tchrist
tchrist

Reputation: 80443

<Address>(?:(?!<Address>)(?!<Number>).)*</Address>

Warning: that assumes that you either have no newlines in your text, or that . can cross newline boundaries, the so-call /s or DOTALL mode, which (?s) will enable in some regex languages.

Upvotes: 2

Related Questions