News for hackers. Stuff that matters.
Re:You can't parse XML with regex. Let's do it anyways by minitech on Saturday October 04, 2025 @11:54PM (#45478717)
What is a “regular (CS sense) string”, especially if it’s apparently from a non-regular language? That’s not a thing. Maybe you mean you can parse regular subsets of non-regular languages, which is true but usually doesn’t lead to a robust solution when it comes to commonly-encountered non-regular data interchange languages like HTML and XML.*

> So we tell people you "can't" parse XML with regular expressions, even though the claim is nonsense if you think about it

It’s not nonsense. The actual meaning is ambiguous and a little context-sensitive (ha ha), but you can’t use a regular expression alone (CS sense) to do any of these things:

- determine whether an XML document is syntactically valid

- match an element, attribute, or text at a particular path under the assumption that the document is well-formed

And of course anything that isn’t possible for XML really isn’t possible for HTML. It is possible to extract the first/nth overall instance of an element that can’t contain itself under the assumption that the document is well-formed, though!

(The article is about non-robust solutions.)

* Edit: In another comment, you specified “so long as you're willing to place a bound on the length (or equivalently, the nesting depth) of that string”, which is obvious, yes, and leads to solutions that are not only not robust but also not even practical.

There are no comments on this comment yet.