Y
HN Search
Hacker News Search
new
|
comments
|
top
|
jobs
unlinkr
searching PlanetScale…
1.
▲
2.
▲
3.
▲
4.
▲
5.
▲
6.
▲
5 ms
·
1.
▲
by
unlinkr
7y ago
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 confusing.
2.
▲
by
unlinkr
7y ago
I'm not familiar with antlr or javacc, but don't they use regular expressions for lexing?
3.
▲
by
unlinkr
7y ago
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 sections nest, so the i
4.
▲
by
unlinkr
7y ago
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!
5.
▲
by
unlinkr
7y ago
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 to perform the lexic
6.
▲
by
unlinkr
7y ago
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 to evaluate if the f
7.
▲
by
unlinkr
7y ago
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 about selecting such
8.
▲
by
unlinkr
7y ago
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.
9.
▲
by
unlinkr
7y ago
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 the question) a
10.
▲
by
unlinkr
7y ago
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 complexity. Writing a lexer b
11.
▲
by
unlinkr
7y ago
I actually meant (?<tag> in order to create a named capture.
12.
▲
by
unlinkr
7y ago
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 regular expression h
13.
▲
by
unlinkr
7y ago
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 have a problem wit
14.
▲
by
unlinkr
7y ago
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.
15.
▲
by
unlinkr
7y ago
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 expressions for the tokeni
16.
▲
by
unlinkr
7y ago
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.
17.
▲
by
unlinkr
7y ago
> 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?
18.
▲
by
unlinkr
7y ago
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
19.
▲
by
unlinkr
7y ago
You don't need to keep track of state to match tokens like XHTML start or end tags.
20.
▲
by
unlinkr
7y ago
That is a valid XHTML tag (if I remember correctly) and can be matched perfectly fine by a regex.
21.
▲
by
unlinkr
7y ago
An element requires a start and end tag, or a self-closing start tag.
22.
▲
by
unlinkr
7y ago
That is not XHTML.
23.
▲
by
unlinkr
7y ago
How do you think an XHTML parser is written? In particular, how does an XHTML parser identify tokens like start and end tags?
24.
▲
by
unlinkr
7y ago
It only works in practice, not in theory.
25.
▲
by
unlinkr
7y ago
The question is about how to identify start and end-tags in XHTML. What would be an appropriate tool for that job?
26.
▲
by
unlinkr
7y ago
The question is about identifying end-tags in XHTML. This is indeed possible with a regex.
27.
▲
by
unlinkr
7y ago
Here is the answer to the question: https://www.cargocultcode.com/solving-the-zalgo-regex/ tl;dr: It can indeed be solved relatively easily with a regex.
28.
▲
by
unlinkr
7y ago
Stack Overflow have some infamous example of "snarky" answers getting highly upvoted - despite being outright wrong. I wrote a bit about such a case here: https://www.cargocultcode.com/solving-the-zalgo-regex