The answers are ridiculing the OP for asking a totally reasonable question.
The answers are ridiculing the OP for asking a totally reasonable question.
The question doesn't specify if the file to match against is unique/one-shot or if it's a general case. Without that info you can largely assume it has to handle any input. The regex will get unwieldy since you have to account for corner cases like:
<!-- <a href="foo"> -->
<div bar='<a href="foo">'></div>In any case, how would you use xpath or CSS to identity self-closing tags? They operate on the parsed tree, not on the token level, and the question is about identifying specific tokens.
That said, this answer frequently gets linked in discussions even where using regular expressions is an entirely valid approach.
A technically fair answer would be to point out that the regex would have to take other tokens like comments, CData etc. into consideration, so it is more like a five-line regex than a one-line regex. If someone recommended a XHTML tokenizer or other tool which could solve the OP's task, that would also be a great answer.
Yes, but that you can tokenize XHTML using regular expressions is not the same thing as being able to use a single regular expression to extract XHTML tokens. Remember that context free languages are a superset of regular expressions. I don't personally know enough about the XHTML syntax to say off the bat whether the syntax can be described with a regular expression, but generally a recursive definition of valid syntax is not possible to express with regular expressions.
> You cannot use a parser, since a parser does not emit tokens but emit the element tree and abstracts away syntactic details like the difference between <x></x> and <x />.
You can use a parser, just not any XHTML parser. The parser would need to be constructed with the objectives in mind, to parse into a data structure that doesn't abstract these details away.
That said, maybe an even simpler solution exists, such as to use several regular expressions to first remove comment and CDATA before matching. I'm not immediately aware of any other cases that would cause problems for the trivial match suggested in the question post.
This is completely wrong. One can also just write their own tokenizer reading one character at a time with a state machine. It's trivial compared to the complexity of the rest of the parser.
Regex is a family of languages each of which can have various implementations. You could have a regex implementation that instead uses mutually recursive functions etc.
What is true is that regexes are typically not turing complete and can be represented with simple state machines.
Isn't this fairly common for stackoverflow nowadays?
I was ridiculed for posting a query about a C++ concept I was trying to learn from one of the authoritative books on the subject - I just couldn't 'get' the syntax being explained.
I persevered and then someone chimed in that, hey, there was a typo in the book's example!