So it seems we agree that for the question the OP is actually asking a regex is the appropriate tool. Great if you can recommend a tool which can solve the task easier. But saying it is "not possible" is unhelpful and…
I'm not familiar with antlr or javacc, but don't they use regular expressions for lexing?
That is just a comment and can be easily scanned by a regex. The CDATA syntax does not have any particular meaning inside an XML comment if that is what you are suggesting. And in any case, neither comments nor CDATA…
Sure. But that would be true for any parsing technique. No parser known to man would be able to produce a valid parse if you start it in the middle of a quoted string!
The job (the question asked) is about recognizing start and end tags in XHTML. These are lexical tokens and therefore regular expressions are a perfectly fine tool for this. Indeed many parsers use regular expressions…
This is what I really hate about the Zalgo answer. It is instilling people some vague sense that regular expressions are somehow bad, wrong and dangerous. But without any real arguments or contexts which would allow you…
You have to distinguish between the different levels of parsing. Regexes are appropriate for tokenization, which is the task of recognizing lexical units like start tags, end tags, comments and so on. The SO question is…
That pattern would indeed match a quoted string. I don't see how it would matter if the quoted string contains something like "<input". It can contain anything except a quote character.
No, you don't need more than a regular expression. If you want to extract elements, i.e. match start tags to the corresponding end tags, then you need a stack-based parser. But just to extract the start tags (which is…
Why would a hand-written lexer have fewer mistakes than a regular expression using an off-the-shelf regex engine? They would need to encode the same lexical grammar, so at that level there is the same amount of…
I actually meant (?<tag> in order to create a named capture.
So how would you tokenize without the use of regular expressions? What more appropriate technique would you use instead? The example you provide in not XHTML so not really relevant for the discussion. But in any case, a…
The problem is it is funny and wrong. Apparently it have given a lot of people really confused ideas about what is possible and what is not possible with regular expressions. If it had been funny and right I would not…
It was supposed to be (?<tag> \w+ ) in order to create a named capture. The <tag> was apparently lost in editing. Thanks for the heads-up.
The problem with the answer is it is wrong. The question is about identifying start-tags in XHTML. This is a question of tokenization and can be solved with a regular expression. Indeed, most parsers use regular…
Yes you can tokenize this with a regular expression and extract the valid start and end tags. If comments in XHTML could nest you would have a problem. But this is not the case.
> Maybe you can detect tags with regex, but maybe you shouldn't... So what do you think would be a more appropriate choice for writing a tokenizer?
I think there are a lot of knee-jerk answer because people see "XHTML" and "regex" in the same sentence and immediately think "not possible". But the actual question is clearly not about matching start tags to end tags…
You don't need to keep track of state to match tokens like XHTML start or end tags.
That is a valid XHTML tag (if I remember correctly) and can be matched perfectly fine by a regex.
An element requires a start and end tag, or a self-closing start tag.
That is not XHTML.
How do you think an XHTML parser is written? In particular, how does an XHTML parser identify tokens like start and end tags?
It only works in practice, not in theory.
The question is about how to identify start and end-tags in XHTML. What would be an appropriate tool for that job?
So it seems we agree that for the question the OP is actually asking a regex is the appropriate tool. Great if you can recommend a tool which can solve the task easier. But saying it is "not possible" is unhelpful and…
I'm not familiar with antlr or javacc, but don't they use regular expressions for lexing?
That is just a comment and can be easily scanned by a regex. The CDATA syntax does not have any particular meaning inside an XML comment if that is what you are suggesting. And in any case, neither comments nor CDATA…
Sure. But that would be true for any parsing technique. No parser known to man would be able to produce a valid parse if you start it in the middle of a quoted string!
The job (the question asked) is about recognizing start and end tags in XHTML. These are lexical tokens and therefore regular expressions are a perfectly fine tool for this. Indeed many parsers use regular expressions…
This is what I really hate about the Zalgo answer. It is instilling people some vague sense that regular expressions are somehow bad, wrong and dangerous. But without any real arguments or contexts which would allow you…
You have to distinguish between the different levels of parsing. Regexes are appropriate for tokenization, which is the task of recognizing lexical units like start tags, end tags, comments and so on. The SO question is…
That pattern would indeed match a quoted string. I don't see how it would matter if the quoted string contains something like "<input". It can contain anything except a quote character.
No, you don't need more than a regular expression. If you want to extract elements, i.e. match start tags to the corresponding end tags, then you need a stack-based parser. But just to extract the start tags (which is…
Why would a hand-written lexer have fewer mistakes than a regular expression using an off-the-shelf regex engine? They would need to encode the same lexical grammar, so at that level there is the same amount of…
I actually meant (?<tag> in order to create a named capture.
So how would you tokenize without the use of regular expressions? What more appropriate technique would you use instead? The example you provide in not XHTML so not really relevant for the discussion. But in any case, a…
The problem is it is funny and wrong. Apparently it have given a lot of people really confused ideas about what is possible and what is not possible with regular expressions. If it had been funny and right I would not…
It was supposed to be (?<tag> \w+ ) in order to create a named capture. The <tag> was apparently lost in editing. Thanks for the heads-up.
The problem with the answer is it is wrong. The question is about identifying start-tags in XHTML. This is a question of tokenization and can be solved with a regular expression. Indeed, most parsers use regular…
Yes you can tokenize this with a regular expression and extract the valid start and end tags. If comments in XHTML could nest you would have a problem. But this is not the case.
> Maybe you can detect tags with regex, but maybe you shouldn't... So what do you think would be a more appropriate choice for writing a tokenizer?
I think there are a lot of knee-jerk answer because people see "XHTML" and "regex" in the same sentence and immediately think "not possible". But the actual question is clearly not about matching start tags to end tags…
You don't need to keep track of state to match tokens like XHTML start or end tags.
That is a valid XHTML tag (if I remember correctly) and can be matched perfectly fine by a regex.
An element requires a start and end tag, or a self-closing start tag.
That is not XHTML.
How do you think an XHTML parser is written? In particular, how does an XHTML parser identify tokens like start and end tags?
It only works in practice, not in theory.
The question is about how to identify start and end-tags in XHTML. What would be an appropriate tool for that job?