yoogeeks
yoogeeks

Reputation: 965

Get the actual XML tag using RegExp

I have an XML which looks something like:

<drawing><some other tags><Picture><some other tags></drawing><drawing><some other tags><Chart><some other tags></drawing>

And I want to extract

<drawing><some other tags><Chart><some other tags></drawing>

Currently I am using this RegExp:

/<drawing>.*?<Chart>.*?</drawing>/g 

However, it is returning me the whole XML, since it is also valid. But I want only the second occurrence, and unable to arrive at the solution. Thanks in advance.

Upvotes: 2

Views: 50

Answers (1)

zx81
zx81

Reputation: 41848

With all the disclaimers about using regex to parse xml, if you want a regex solution, use this:

<drawing>(?:(?!</drawing>).)*?<Chart>.*?</drawing>

See the match in the Regex Demo.

Explanation

  • <drawing> matches literal chars
  • (?:(?!</drawing>).) matches one character that does not start </drawing>
  • *? repeats this match lazily up till...
  • <Chart> matches literal chars
  • .*? lazily matches chars up till...
  • </drawing>

Upvotes: 1

Related Questions