4 ms·
> Stack Overflow has answers like: https://stackoverflow.com/a/1732454 https://stackoverflow.com/a/1732454 Nowadays heavily discouraged, and would probably be
by Izkata 8y ago
> Stack Overflow has answers like: https://stackoverflow.com/a/1732454 https://stackoverflow.com/a/1732454
Nowadays heavily discouraged, and would probably be deleted as unconstructive; that one still exists because of historical significance/was grandfathered in.
- pugworthy 8y agoI'm not a big fan of that kind of moderation style at all
- gcb0 8y agonobody is. just the moderators. it's recursive.
- kapep 8y agoI'm fine with it. It's kind of the same as here on HN. If unconstructive comments weren't discouraged here, the comments section here would look like on Reddit. If unconstructive answers weren't discouraged on SO, I'm quite sure the more popular questions would be full of flippant half-answers trying to be funny or people posting memes to grab attention.
- boomlinde 8y agoFor a discussion group I might agree, but SO is supposed to be a repository of legitimate questions and their legitimate answers, not a place for general banter.
- goto11 8y agoTo be fair it is unconstructive. If you read the question carefully, it is clear is can be solved using a regex because it is about identifying tokens, not about parsing them into a tree. Parsers typically use regexes for the tokenization stage - indeed, what else would you use? The answers are ridiculing the OP for asking a totally reasonable question.
- lloeki 8y agoRegexes are indeed a perfectly fine answer when you have the guarantee no corner cases will show up in the content, and I did and still do use regexes to quickly extract data form well-known HTML/XML as a quick hack (curl|grep). Otherwise you're much better served by using a parser and select nodes with xpath/css. 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>
- goto11 8y agoThe second line is not a corner case, that is simply not legal XHTML. You cannot have an unescaped < in an attribute value. You will need to take comments (and DTD's and CData) into consideration of course, but you can do that in a regex. 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.
- mayniac 8y ago>The answers are ridiculing the OP for asking a totally reasonable question. Isn't this fairly common for stackoverflow nowadays?
- linker3000 8y agoHell yeah: 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!
- boomlinde 8y agoMaybe not very constructive, but I think it's a technically fair answer given the question. The person asking is not intending to match individual tokens one by one to feed into a parser, but simply to use a regular expression to extract all instances of a set of opening tags in a whole document. The trivial solution he proposes, while perfectly sufficient for some subset of documents, quickly breaks in the general case when you consider comments and CDATA sections. For that you need to maintain an understanding of the whole document. That said, this answer frequently gets linked in discussions even where using regular expressions is an entirely valid approach.