The comment matching subexpression is way too liberal. How it really works is that <! opens a markup declaration, and within such, a -- double dash starts a comment and another one terminates the comment. In other words, <!-----> (5 dashes) cannot be part of a well-formed document: <! starts the declaration, -- starts a comment, -- closes it, but then there’s an extraneous - dash which is outside the comment and is a markup declaration syntax error. OTOH, <!------> (6 dashes) is fine, except it will comment out much more than you think, because the first 4 dashes open an close an empty comment, and then the last two dashes open a comment that is not closed at this point. However, if you write something like <!--- --> (3 dashes, space, 2 dashes), that’s fine: the first 2 dashes open the comment, the next dash is lone so it’s part of the comment (along with the space that follows), and the last 2 dashes close the comment.
Likewise, your parsing for the XML PI is too simpleminded; additionally, you have no provision for parsing other kinds of PIs at all.
You may have more errors; I didn’t look any harder than that.
You’d do well to just read the spec; it really isn’t prohibitively big or difficult to understand, and if you want to write a correct parser, there’s no way around it.
Because despite claims to the contrary, what you’re doing is parsing. And regexen are certainly a fine tool to do that. The oft-repeated advice is because people who use regexen usually don’t actually parse, and in general, do not have a task that merits the effort required to write a complete, correct parser on their own either, so telling them to use an existing parser is exactly what they need.
Makeshifts last the longest.
In reply to Re: Tokenizing XML
by Aristotle
in thread Tokenizing XML
by Skeeve
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |