News for hackers. Stuff that matters.
Re:You can't parse XML with regex. Let's do it anyways by xyzzy_plugh on Saturday October 04, 2025 @10:54PM (#45478523)
One of my first jobs was parsing XML with Regular Expressions. Like TFA the goal was not to construct the entire document tree, but rather extract data. It worked great!
Re:You can't parse XML with regex. Let's do it anyways by electroly on Saturday October 04, 2025 @11:12PM (#45478588)
For years and years I ran a web service that scraped another site's HTML to extract data. There were other APIs doing the same thing. They used a proper HTML parser, and I just used the moral equivalent of String.IndexOf() to walk a cursor through the text to locate the start and end of strings I wanted and String.Substring() to extract them. Theirs were slow and sometimes broke when unrelated structural HTML changes were made. Mine was a straight linear scan over the text and didn't care at all about the HTML in between the parts I was scraping. It was even an arbitrarily recursive data structure I was parsing, too. I was able to tell at each step, by counting the end and start tags, how many levels up or down I had moved without building any tree structures in memory. Worked great, reliably, and I'd do it again.
Re:You can't parse XML with regex. Let's do it anyways by thaumasiotes on Saturday October 04, 2025 @11:01PM (#45478548)
Why regular expressions? Why not just substring matching?
Re:You can't parse XML with regex. Let's do it anyways by th0ma5 on Saturday October 04, 2025 @11:28PM (#45478636)
This, much more deterministic!
Re:You can't parse XML with regex. Let's do it anyways by bazoom42 on Sunday October 05, 2025 @04:19PM (#45484871)
Not sure if you are joking, but regexes are deterministic.
Re:You can't parse XML with regex. Let's do it anyways by th0ma5 on Sunday October 05, 2025 @04:58PM (#45485199)
Oh, no I didn't mean to say regex or not, I meant regex over XML vs regex over a string. The first has the illusions everyone is bringing up that XML is not regular, but having clarity that it is ultimately a string is the correct set of assumptions.
Re:You can't parse XML with regex. Let's do it anyways by quotemstr on Saturday October 04, 2025 @10:58PM (#45478544)
It really is a central example of the bell curve meme, isn't it?

The reason we tell people not to parse HTML/XML/whatever with regular expressions isn't so much that you can't use regular (CS sense) patterns to extract information from regular (CS sense) strings that happen to be drawn from a language that can also express non-regular strings, but because when you let the median programmer try, he'll screw it up.

So we tell people you "can't" parse XML with regular expressions, even though the claim is nonsense if you think about it, so that the ones that aren't smart and independent-enough minded to see through the false impossibility claim don't create messes the rest of us have to clean up.

One of the most disappointing parts of becoming an adult is realizing the whole world is built this way: see https://en.wikipedia.org/wiki/Lie-to-children

Re:You can't parse XML with regex. Let's do it anyways by ntcho on Sunday October 05, 2025 @12:02AM (#45478750)
This reminds me of cleaning a toaster with a dishwasher: https://news.ycombinator.com/item?id=41235662
Re:You can't parse XML with regex. Let's do it anyways by mjevans on Saturday October 04, 2025 @11:05PM (#45478569)
It's always the edge cases that make this a pain.

The less like 'random' XML the document is the better the extraction will work. As soon as something oddball gets tossed in that drifts from the expected pattern things will break.

Re:You can't parse XML with regex. Let's do it anyways by quotemstr on Saturday October 04, 2025 @11:08PM (#45478578)
Of course. But the mathematical, computer-science level truth is that you can make a regular pattern that recognizes a string in any context-free language so long as you're willing to place a bound on the length (or equivalently, the nesting depth) of that string. Everything else is a lie-to-children (https://en.wikipedia.org/wiki/Lie-to-children).
Re:You can't parse XML with regex. Let's do it anyways by rcxdude on Sunday October 05, 2025 @06:18AM (#45480431)
You can, but you probably shouldn't since said regex is likely to be very hard to work with due to the amount of redundant states involved.
Re:You can't parse XML with regex. Let's do it anyways by quotemstr on Sunday October 05, 2025 @11:08AM (#45482058)
Our discourse does a terrible job of distinguishing impossible things from things merely ill-advised, and I think intellectual honestly requires us to be up front about the difference.

Yeah, I'd almost certainly reject a code review using, say, Python's re module to extract stuff from XML, but while doing so, I would give every reason except "you can't do that".

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.

Re:You can't parse XML with regex. Let's do it anyways by zeroimpl on Saturday October 04, 2025 @11:10PM (#45478580)
If I’m not mistaken, even JSON couldn’t be parsed by a regex due to the recursive nature of nested objects.

But in general we aren’t trying to parse arbitrary documents, we are trying to parse a document with a somewhat-known schema. In this sense, we can parse them so long as the input matches the schema we implicitly assumed.

Re:You can't parse XML with regex. Let's do it anyways by quotemstr on Saturday October 04, 2025 @11:12PM (#45478586)
> If I’m not mistaken, even JSON couldn’t be parsed by a regex due to the recursive nature of nested objects.

You can parse ANY context-free language with regex so long as you're willing to put a cap on the maximum nesting depth of constructs in that language. You can't parse "JSON" but you can, absolutely, parse "JSON with up to 1000 nested brackets" or "JSON shorter than 10GB". The lexical complexity is irrelevant. Mathematically, whether you have JSON, XML, sexps, or whatever is irrelevant: you can describe any bounded-nesting context-free language as a regular language and parse it with a state machine.

It is dangerous to tell the wrong people this, but it is true.

(Similarly, you can use a context-free parser to understand a context-sensitive language provided you bound that language in some way: one example is the famous C "lexer hack" that allows a simple LALR(1) parser to understand C, which, properly understood, is a context-sensitive language in the Chomsky sense.)

The best experience for the average programmer is describing their JSON declaratively in something like Zod and having their language runtime either build the appropriate state machine (or "regex") to match that schema or, if it truly is recursive, using something else to parse --- all transparently to the programmer.

Re:You can't parse XML with regex. Let's do it anyways by LegionMammal978 on Sunday October 05, 2025 @10:27AM (#45481740)
What everyone forgets is that regexes as implemented in most programming languages are a strict superset of mathematical regular expressions. E.g., PCRE has "subroutine references" that can be used to match balanced brackets, and .NET has "balancing groups" that can similarly be used to do so. In general, most programming languages can recognize at least the context-free languages.
Re:You can't parse XML with regex. Let's do it anyways by Crestwave on Saturday October 04, 2025 @11:29PM (#45478640)
It's impossible to parse arbitrary XML with regex. But it's perfectly reasonable to parse a subset of XML with regex, which is a very important distinction.
Re:You can't parse XML with regex. Let's do it anyways by ok123456 on Sunday October 05, 2025 @12:27AM (#45478839)
Can regular expressions parse XML: No.

Can regular expressions parse the subset of XML that I need to pull something out of a document: Maybe.

We have enough library "ergonomics" now that it's not any more difficult to use a regex vs a full XML parser now in dynlangs. Back when this wasn't the case, it really did mean the differnce between a one or two line solution, and about 300 lines of SAX boiler-pate.