12

Whenever someone answers a regex question they (of course) give the correct answer, but there is usually never an explanation of what is happening. Sometimes I think the answer is very clever, but unless you know regex already you won't know that.

Someone just gave this for this question:

str.replace(/( )|[ \t\n]/g, '')

...as an answer, and it works specifically for what the questioner wanted, so it makes it completely limited to one exact thing?

So my question is... can everyone include some form of explanation of what the regex is doing piece by piece? (You can still answer it quick then go back and expand on it later.)

8
  • 2
    This belongs on meta. And I completely agree, when giving an answer, an answer is more useful if it includes an explanation (and ideally a link to relevant reference material). Dec 30, 2010 at 9:36
  • Not only that, but in the given example a \s would even be more appropriate ;)
    – mplungjan
    Dec 30, 2010 at 9:39
  • @mplungjan why would a \s be even be more appropriate?
    – gravityboy
    Dec 30, 2010 at 9:52
  • In other words, we need more people like this fellow: stackoverflow.com/users/276101/polygenelubricants Dec 30, 2010 at 9:52
  • @gravityboy: I guess it's because \s represents any kind of whitespace including \t and \n. Dec 30, 2010 at 9:53
  • @BoltClock yes this answer he gave here... stackoverflow.com/questions/3171671/… has a fantastic explanation
    – gravityboy
    Dec 30, 2010 at 9:55
  • @gravityboy Actually \h is the proper replacement for [ \t\n].
    – mario
    Dec 30, 2010 at 15:42
  • This is the “Fastest Gun in the West” problem. Some types of question attract it more than others. Dec 31, 2010 at 17:27

3 Answers 3

7

This is one reason why we recently increased the minimum valid post size from 15 characters, to 30 characters.

Another reason is for competitive upvoting -- if comparing three answers:

  1. correct with no explanation
  2. correct with a reasonable explanation
  3. correct with a long and authoritative explanation

Generally #2 and #3 are going to "win" pretty decisively. There is obviously a tradeoff in the amount of time you spend on the answer. Of course, if you enjoy writing a great answer -- and who doesn't -- then that is its own reward, but I tend to favor #2 in most cases as a good balance.

1
  • Yes I noticed #2 and #3 will win but with the regex the answer is usually blasted up really quick and the asker tries it and it works so it is accepted. The asker might not even know why it works? ... but they are still happy. That's why I was thinking... blast the regex answer up quick... get the vote then comeback and give a decent explanation. p.s. I tried to vote you up but I just signed up for this meta so it wouldn't let me.
    – gravityboy
    Dec 30, 2010 at 10:41
3

I'd like to disagree with your appeal.

The original question was a very valid newcomer question and the result of the syntax complexity of regular expressions. The OP simply mixed up [] and (). A good answer would have explained that. (And well, one actually did.)

Yet, the majority of regex questions I see are more of the plzsendtehcodez kind. And I don't think it's appropriate to replicate the handbook on every occasion. Yes, detailed explanations would be helpful, but regexps are sort of a programming language of their own. If you demand explaining every single placeholder and piece by piece, that should imply that every syntax token in C and C++ or Java code examples had to be explained as well.

My point being, if you ask on Stackoverflow you should have at least mediocre experience in the language you are inquiring about. The Q&A scheme isn't suitable for introductions and getting to know the syntax. If askers are genuinely interested in the topic, they will RTFM once they got an interesting answer. But since the majority are just interested in ready solutions, uncommented ready solutions often suffice.

2
  • Okay, just broke my own advise. But then this question was the rare exception which asked for explaining a regex.
    – mario
    Dec 30, 2010 at 21:32
  • there is also some discussion of "canonical questions" where the question is "build me a mod_rewrite" or "build me a regex" or "build me an IP subnet mask" -- in other words, questions that are almost all the same thing and differ only in the narrow specifics of what they want built. We might have a master "here's how you learn regex" question that we start closing some of the more egregious ones as, since there are an infinite # of them. Dec 31, 2010 at 1:58
2

First, the posted question has more than one answers with good explanations. Codaddict's answer came 40 minutes before your question.

I don't think every answer should explain every aspect of the regex. It should explain why the original regex failed, and the tricky part of the answer, if such exists.
In this case you don't have anything besides simple, first-lesson features of regular expressions: |, \s and +, and a short explanation about why [ ] doesn't work.

Also, remember - if you think something is missing, feel free to ask!

6
  • The problem with giving a "late", detailed answer is that very often it goes almost ignored. The first working answer gets several votes, and often the OP rushes to mark it accepted and/or walks off. See pthis](stackoverflow.com/questions/4563971/…) recent example where I spent a couple minutes typing up a decent explanation, was 3 minutes after the first answer (which was initially missing the ?s, so incorrect) and resulted in 0 votes where @BoltClock got 6.
    – moinudin
    Dec 30, 2010 at 16:05
  • @marcog - Being first is part of the game :) - add a quick, correct answer, and expand later (you even get 5 minutes before it registers). Also, the OP cannot mark the answer in the first 20(?) minutes, so there's plenty of time for other answers.
    – Kobi
    Dec 30, 2010 at 16:12
  • @Kobi I fully realise that. What I'm trying to say is there's too much emphasis on being fast over giving a detailed answer that unless you're way ahead on the latter, there's little benefit and as a result of the lack of voting the detailed answer is often lost in the mess of 5-10 no-detail answers. Many newcomers will often accept the highest voted answer, making the 20 minute period moot in many cases.
    – moinudin
    Dec 30, 2010 at 16:20
  • (@marcog also) yes... and if you are digging through posts months later looking for something similar, you might go with the answer that has the highest votes... but it might not actually be the best answer. And it's hard not to trust it as the best because it has the highest votes.
    – gravityboy
    Dec 31, 2010 at 4:33
  • p.s. what is actually the best answer for that regex question? stackoverflow.com/questions/4561174/… Codaddict's answer was later so it didn't get voted up as much as it might have?
    – gravityboy
    Dec 31, 2010 at 4:38
  • @Kobi: Just remember that other people play the game completely differently. For example, I won't vote for any answer that I feel is too short and I don't race to give an answer; there's always room to give at least some guidance so that other people can learn how to solve things themselves. Dec 31, 2010 at 17:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .