Alex Rivera | Logout

Why is it such a bad idea to parse XML with regex?

Asked 2011-12-20T14:34:40.020
31

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?

Edit
Report

1 Answer

6

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.

answered 2011-12-20T16:54:44.250

Your Answer