Daniyal
Daniyal

Reputation: 905

Removing XML-tag via regex

Greetings, I've read some threads about this topic, but actually I was unable to find or to think of a adequate solution (see for example: Regular expression to remove XML tags and their content).

I have an xml tag like that:

<bla_tag size="100"
         diameter="50"
         ratio="0.2"
         path="/user/home/something.pdf">
</bla_tag>

Aim: Having a regular expression that removes everything in between <bla_tag ...> .

Problem: the values like size, etc. change in each of the bla_tags (about 1000 bla-tags in the file).

Failed attempt: I tried it with: <bla_tag .*?> (the ? to make it less greedy...). Result of failure: Only <bla_tag has been marked, but not the content within the entire bracket!

What am I doing wrong - or is it actually possible to solve this problem based on regex (I read somewhere that it would not be possible due to xml property to be an type-2 language, can you confirm that?)

Upvotes: 2

Views: 3794

Answers (1)

Arafangion
Arafangion

Reputation: 11950

You want to read RegEx match open tags except XHTML self-contained tags

Seriously.

Use an xml parser. (They're not hard to use, honestly). They generally come in one of two flavours - SAX, and DOM, and you're probably going to prefer SAX.

My favorite parser is expat, but they all each have their little subtleties so it's not always a one-size-fits-all.

Upvotes: 5

Related Questions