3 ms·
No one claimed that it's impossible to use regexes in the process of parsing XML. It is however impossible to parse arbitrary XML with a single regex. For exam
by Ives 15y ago
No one claimed that it's impossible to use regexes in the process of parsing XML. It is however impossible to parse arbitrary XML with a single regex.
For example: write a regular expression that will return the contents of the first <div> that is found in the following XML:
For example:
<div>
whatever
<div>
more stoff
</div>
foo
</div>
or
<div>
whatever
foo
</div>
You can't because XML is not a regular language and regexes only match regular languages.
- evincarofautumn 15y agoEvery specific counterexample can be countered with a specifically tailored countersolution. If you only need to parse a certain subset of an XML document, then you don’t need to parse XML, and (ordinary) regular expressions will work. Also, the article does say that you can parse well-formed XML with a single regex, you just can’t validate it that way.
- SimHacker 15y agoBy "just fine" do you mean "not quite"? You missed the part about the first job of a parser being "check for correct syntax": http://en.wikipedia.org/wiki/Parser http://en.wikipedia.org/wiki/Parser Parser In computing, a parser is one of the components in an interpreter or compiler, which checks for correct syntax and builds a data structure (often some kind of parse tree, abstract syntax tree or other hierarchical structure) implicit in the input tokens. The parser often uses a separate lexical analyser to create tokens from the sequence of input characters. Parsers may be programmed by hand or may be (semi-)automatically generated (in some programming languages) by a tool.