I have thought about this a lot and I have come to some conclusions.
It's often best to ask for clarifications
Firstly, before deciding if you should answer the actual question, propose a better method, or both, remember that it's never a bad thing to use the comment section to ask for clarifications. Many misunderstandings can be avoided by simply asking "Regex does not seem to be a good solution here. Are you sure you really want that, or are you open for other solutions too"
When they have answered, you can ask them to edit the question to add either "It's possible that regex is not a good solution, but it is a requirement from my professor/employer" or "I thought regex was the way to go, but I'm completely open for alternate solutions". Or you can simply edit the question yourself after OP has clarified it.
Blindly answering the exact question
It can be a bad thing to not point out flaws in the method OP wants to use. Blindly just answering the question can mislead both OP and future readers into writing bad code. If it's clear that the proposed method is indeed a bad method, then it should be pointed out. But it's always a good thing to inform about pros and cons with both the proposed method and alternate methods.
Refusing to answer the question
But it can also be a bad thing to simply refuse to answer the actual question for several reasons. First and foremost, simple curiosity IS a valid use case. There are a lot of questions where the aim simply is to understand details and corner cases better. These often have the tag language-lawyer. Here is an example about that Is it safe to do something like foo(x, &x)? where the use case was to be able to improve another answer I was writing.
However, there is also often the case that it's a matter of opinion. Also, the use case may be something different than technical reasons. A prime example was when I was wondering about using char[]
for passwords instead of String
in Java. It was this question: How do I send a password without using String from java to postgresql?
Granted, I learned a lot. But here the use case was that our customers simple required us to use char[]
. It didn't matter if it was a pointless requirement. It was still a requirement I needed to follow, and that is a valid use case.
Analyzis
I think a very important thing is to avoid lecturing people. Help them, but do not lecture them. It's a very big difference in how you express yourself. I think these are completely ok:
Other answers have given good examples of how to do this with a regex, so I won't go into details about that. Instead, I'd like to suggest an alternate method. Notice how easy it is.
A regex is not very suitable here. Instead, I'd do like this:
But just for completeness, here is a regex that does what you want:
The answer relies on other answers for completeness, or include a short solution.
An answer that does not answer the question, but is still ok:
I did try to find a regex solution, but I failed. It was trickier than I thought, but I did manage to solve it like this. Hope it helps.
If it's really that hard to find a regex that does it, then it's a very good sign that it's not a good solution. I think that's a completely ok answer. No objections.
Slightly more pushy, but still ok in my opinion:
A regex is not a very preferable solution here. Sure it's possible to use, but the time complexity would be horrible, and it will be very hard to maintain it. Instead, please consider this:
It does not lecture. It simply tells what the drawbacks are, which is ok. If OP has explicitly said that they want a regex, then it would be preferable to add a sentence like "I know you wrote that you really want a regex, but for future readers, I'd really like to add a few lines about why it's a bad idea"
But this is an answer that might be a bit to lecturing:
Regex is the wrong way to go here. Instead, use this.
No explanation. Just stating that you should not do it. This can easily be perceived as opinions from those who blindly follows what they believe is best practice. In my opinion, this is very much on the edge of what is acceptable. If OP has explicitly stated that they want a regex, then I would definitely consider this a bad answer that deserves a down vote, and possibly even a not an answer flag.
Writing an answer without answering the actual question can be completely ok. But if you get the response "Thank you, but I really want to solve this with method X" then take a step back.
But it's quite tricky to come up with a simple rule that is applicable for all cases. It has to be judged from case to case. But both blindly answering the question without informing about the drawbacks and refusing to answer the question can deserve a down vote. It depends on the situation.
Conclusion
The two best things are:
asking OP for clarifications
answer the actual question, explain why the method is bad, and propose a better method
while
withgoto
, you cannot parse HTML with regex.