Anyone who has stopped for more than 5 seconds in the regex tag knows of this "dubious" answer:
Every time you attempt to parse HTML with regular expressions, the unholy child weeps the blood of virgins, and Russian hackers pwn your webapp.
I know that it's funny. I know this answer will never be deleted because it has historical value.
But, whether you love this answer or hate it, whether you think it's funny or vitriolic, the plain truth is that it doesn't represent a good answer:
For that reason alone, I think it would be best if fellow SO users voluntarily stopped linking to it via comments in favor of more relevant, higher-quality answers.
I am not asking that this link be banned in any way, shape or form.
I know that users are constantly posting these questions and they get annoying. We already have approved ways of dealing with that: downvote, close (not constructive, too localized, duplicate) and delete. If your goal in linking to that answer is to indicate to the OP that the question shouldn't have been asked, then perhaps you would consider sticking to downvoting and voting to close.
There have been over 1,100 links to this question/answer since 2008 (that's nearly 2% of all questions tagged with regex - and that includes questions before this answer was posted). There have been nearly 150 this year alone.
In the cases I've seen, the OP was relatively new or clueless about regexes. This answer does not help them - it only contains a very weak suggestion at the end that a parser should be used (without ever actually explaining why regexes won't work - a very ineffective argument for the determined "n00b"). In my opinion, to many new users, it could easily come off as rude, caustic or snooty. I don't think that's the reputation we want for StackOverflow.
To be clear: parsing HTML with the help of regex, while possible in some regex flavors, should be highly discouraged. The cases where regex is an easier/cheaper tool than a true HTML parser are few and far between. That is not the issue... the issue is that this particular answer, at the very, very least, according to our own standards, is not a good answer.
To be clear: I am not suggesting that we hold newbies' hands. I am merely pointing out that this answer is not a good answer according to the standards published in the FAQ.
Our standards for good answers:
- Answer the question
Read the question carefully. What, specifically, is the question asking for? Make sure your answer provides that – or a viable alternative. The answer can be “don’t do that”, but it should also include “try this instead”. Any answer that gets the asker going in the right direction is helpful, but do try to mention any limitations, assumptions or simplifications in your answer. Brevity is acceptable, but fuller explanations are better.
This question just barely squeaks by with a "Have you tried using an XML parser instead?" at the end. Beyond this, there was no attempt to explain the issues or the alternatives.
- Writing style matters
Nobody’s perfect — but answers with correct spelling, punctuation, and grammar are easier to read. They also tend to get upvoted more frequently. Remember, you can always go back and edit your answer to improve it at any time.
The spelling and grammar are intentionally bad. While it works toward the comedic affect of the post, I don't think SO is meant to be a comedy house.
- Always be polite and have fun
It’s fine to disagree and express concern, but please be civil. There’s a real human being on the other end of that network connection, however misguided they may appear to be. We’re here to learn from our peers, not yell at each other.
Please, someone, explain to me how this answer could possibly be construed as civil or polite. If I gave an answer like this to my manager, I'd probably get a reprimand.
- You cannot do that in X because it doesn't support Y.
There are no links to documentation (which are requested if possible) even though plenty of documentation exists.
- This is what you're doing incorrectly. [Explain]. Here's how it should be done [code].
This answer does not attempt to explain the issue or to provide the correct approach.
- Try this: [code]
Again, no attempt to explain
- This is the problem: [Explain]. In a related answer/blog post, I/they discuss blah [link] which might be of interest.
Again, no attempt at explanation.
There are plenty of good answers which already exist. Not the least of which is this one (Regular expression pattern not matching anywhere in string) which has answers that actually answer the question and then explain, in exceptional detail, why this path is not ideal in most cases.
As noted in the comments/answers below, there is an especially good resource question available for PHP-specific HTML parsing questions: How do you parse and process HTML/XML in PHP?
perl expressions
. tchrist is smart, and he is a leading authority on perl but he is also pompous in the extreme. I'm sorry but I have to disagree with tchrist and you. In none of the links here or in your discussion with him, did he show me any evidence that a regex by its self can parse html.