开发者

Removing XML-tag via regex

开发者 https://www.devze.com 2023-01-18 12:01 出处:网络
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).

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 th开发者_StackOverflow社区at:

<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?)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消