I was just reviewing a previous post I made and noticed a number of people suggesting that I don't use Regex to parse xml. In that case the xml was relatively simple, and Regex didn't pose any problems. I was also parsing a number of other code formats, so for the sake of uniformity it made sense. But I'm curious how this might pose a problem in other cases. Is this just a 'don't reinvent the wheel' type of issue?
相关问题
- Illegal to have multiple roots (start tag in epilo
- Newtonsoft DeserializeXNode expands internal array
- how to use special characters like '<'
- XML - XSLT - document() function inside count() fu
- convert logback.xml to log4j.properties
相关文章
- Creating XML Elements without namespace declaratio
- Get Attribute Value From Simple XML Using JQuery /
- Optimization techniques for backtracking regex imp
- Regex to check for new line
- Allow only 2 decimal points entry to a textbox usi
- Directly signing an Office Word document using XML
- Comparing speed of non-matching regexp
- When sending XML to JMS should I use TextMessage o
XML is not a regular language (that's a technical term) so you will never be able to parse it correctly using a regular expression. You might be successful 99% of the time, but then someone will find a way of writing the XML that throws you.
If you're writing some kind of screen-scraper then a 99% success rate might be adequate. For most applications, it isn't.
The real trouble is nested tags. Nested tags are very difficult to handle with regular expressions. It's possible with balanced matching, but that's only available in .NET and maybe a couple other flavors. But even with the power of balanced matching, an ill-placed comment could potentially throw off the regular expression.
For example, this is a tricky one to parse...
You could be chasing edge cases like this for hours with a regular expression, and maybe find a solution. But really, there's no point when there are specialized XML, XHTML, and HTML parsers out there that do the job more reliably and efficiently.
This has been discussed so many times here on SO. See e.g.
Can you provide some examples of why it is hard to parse XML and HTML with a regex?
Why it's not possible to use regex to parse HTML/XML: a formal explanation in layman's terms
Just follow the links on the right side of the screen to more answers.
My conclusion:
Simple, because a regular expression is not a parser, its a tool to find patterns.
If you want to find a very specific pattern in a (ht|x)ml file, go on, regex is perfect for that.
But if you are searching for something in in every Foo tag, that could have attributes in different orders, that can be nested, that can be malformed (and still valid), then use a parser, because thats not pattern matching anymore.