15

I asked How can I split a string except when the delimiter is protected by quotes or brackets? and it was downvoted. What did I do wrong? I've followed the points in How do I ask a good question?:

  1. Write a title that summarizes the specific problem

    How can I split a string except when the delimiter is protected by quotes or brackets?

  2. Introduce the problem before you post any code

    I asked How to split a string with conditions. Now I know how to ignore the delimiter if it is between two characters.

    How can I check multiple groups of two characters instead of one?

  3. Search, and research ...and keep track of what you find

    I found Regex for splitting a string using space when not surrounded by single or double quotes, but I can't understand where to change '' to []. Also, it works with two groups only.

  4. Include just enough code to allow others to reproduce the problem

    // Input
    "text1":"text2","text3":"text,4","text,5":["text6","text,7"],"text8":"text9","text10":{"text11":"text,12","text13":"text14","text,15":["text,16","text17"],"text,18":"text19"}
    // Output
    "text1":"text2"
    "text3":"text,4"
    "text,5":["text6","text,7"]
    "text8":"text9"
    "text10":{"text11":"text,12","text13":"text14","text,15":["text,16","text17"],"text,18":"text19"}
    
  5. Include all relevant tags

15
  • 2
    Just a guess but I think this is an edge of "what have you tried". Ie. you ask HOW to do something but it seems like you have not done any research nor tried anything yourself...Hover over the downvote for an indication why people may have downvoted..
    – user2140173
    Commented Aug 19, 2014 at 7:01
  • 1
    a definite answer to your own case does not exist. People vote however they want and unless someone who actually downvoted comes around and explains why they downvoted you will never find out. Sorry, general advices for improving downvoted questions are provided here
    – user2140173
    Commented Aug 19, 2014 at 7:33
  • well, just have a look at my reputation tab and explain all the recent downvotes to me....It is what it is and if you have an idea of improving it, share with us
    – user2140173
    Commented Aug 19, 2014 at 7:46
  • 12
    Why on earth are people down voting questions asking for help? We want good questions. The OPs question may not be rescuable (I haven't looked) but they obviously want to learn how to ask a good question, which should be applauded. Or, would you rather they continued asking "bad" questions?
    – Ben
    Commented Aug 19, 2014 at 7:58
  • 5
    One reason may be that tons of questions pour in everyday asking how to write regex, and these get downvoted. Generally, it shows lack of research by users who use SO as a regex writing service. Commented Aug 19, 2014 at 8:28
  • 1
    @InfiniteRecursion I shared my research.
    – user3453226
    Commented Aug 19, 2014 at 8:32
  • 4
    @Ben there is completely nothing wrong with willing to learn to improve/ask good questions. Help centre is the starting point though. The problem is that if all the people who got a downvote on their question created a new topic on meta for their OWN SEPARATE CASE then the rule of thumb - search and research is not in the picture any longer. There are answers to similar/duplicate questions here already - "how do I improve" YES, plenty of duplicates but "WHY WAS MY Q DOWNVOTED" - unexplainable, non-answerable unless you're the downvoter willing to explain the reason for your vote.
    – user2140173
    Commented Aug 19, 2014 at 9:34
  • @mehow Note the specific-question tag.
    – user3453226
    Commented Aug 19, 2014 at 11:53
  • @Joiner yeah added a few minutes ago....
    – user2140173
    Commented Aug 19, 2014 at 11:54
  • 1
    @mehow You said: "OWN SEPARATE CASE". It's ok! Because there is a tag such as specific-question.
    – user3453226
    Commented Aug 19, 2014 at 11:59
  • 2
    @mehow People are trying to get newer users to ask questions like these on Meta. Closing a question like this as a duplicate is just counterproductive, and does not help the OP in this case. Try to have a little patience, or just skip over these questions if you can't. Clearly the OP has had some sort of interaction with the help center, due to how they broke this question down.
    – Kendra
    Commented Aug 19, 2014 at 13:44
  • 1
    @Kendra I understand where you are coming from but in this case (like Ive already said) - if you want to improve your question then this is a duplicate - if you want to know the downvotes - you can't unless the downvote explains his own reason.
    – user2140173
    Commented Aug 19, 2014 at 13:46
  • 2
    @mehow While the topic itself is a duplicate, if we want new users to be able to ask for help improving their questions, we can't point to canned advice for a completely different SO question. In cases where it's clear they did not read the help center, that might be a better idea. But in this case the OP appears to have read the help center and tried to conform to it. So it would be more productive and helpful to the OP in this case to look at their specific SO question to help them improve that. At this point, they've removed the question about downvotes, so don't address that part.
    – Kendra
    Commented Aug 19, 2014 at 13:48
  • @mehow I do understand your point, and I agree in some cases that's better, but in cases where they've clearly read what we normally point them to first (help center) and they're still not getting it, we should in turn make the effort to attempt to help them understand, shouldn't we?
    – Kendra
    Commented Aug 19, 2014 at 13:51
  • 4
    Honestly, I think if you'd like a good explanation, go find the biggest java and regex chat rooms and ask there. Those are the users downvoting you (I'd suspect).
    – user1228
    Commented Aug 19, 2014 at 15:20

2 Answers 2

12

I think the main issue is visible here:

"...I am only 15 so my knowledge is low..."

I want to start by encouraging you to continue with what you're doing. I was a young programmer once and I remember struggling with basic concepts, trying to make big things happen with my very small "toolset" of knowledge. There's a LOT to learn, too much for a lifetime of study. But you will progress, you will get better and better and the things that you struggle with today will be easy in a little while.

However, this site was explicitly designed for "professional and enthusiast programmers". The purpose is not to answer any one person's question (that would be a forum), but to create a collection of questions and answers that will be helpful and useful to other programmers in the future. Look at the downvote description:

enter image description here

That's a list of "OR" statements - meeting just one of those criteria makes a post eligible for a downvote. The "not useful" part is probably what played into the downvotes the most, especially given that this is a regex question (a topic much more frequently abused then others). Reading your question, as a professional, I see no real value in it for other programmers. It's been answered many, many times already in many different forms, though finding an exact duplicate would be basically impossible because the requirements are so narrow.

Stack Overflow is very much like an advanced philosophy site. Questions about things that have been around for a long time and about topics that have been discussed at length are not very useful, and thus may attract downvotes for that reason and that reason only. After a while, even philosophy teachers get tired of answering "What if I'm just a brain in a jar?"-type questions, and many users on this site have gotten tired of answering "How can I fine-tune this regex to do what other technologies are better at doing, the result of which will be useful to no one but me."

Simple questions are absolutely permitted, because the art is constantly changing under our feet and even professionals need to ask a basic question every now and then. Had yours been one of the first questions about regexes, it probably would have been well received. But it wasn't.

I think you may find an answer to a similar question helpful:
https://meta.stackexchange.com/a/180715/191410

But don't let any of this dissuade you from asking questions on Stack Overflow!

There is no way to learn except by experience, and the experience you are gaining is extremely valuable. Now that you know other technologies exist for accomplishing your goal, you have become a better programmer and the world is just a little bit better for that. I think that's worth a few downvotes, don't you?

2
  • What dissuades me from asking questions is the automatic banning system.
    – user3453226
    Commented Aug 22, 2014 at 7:00
  • @Joiner - A lot of people feel very concerned about that, but you don't need to be. It's designed to kick in only for "accounts with a history of extremely poor posts". A few downvotes now and then will not put you in any danger, especially if you have posts with upvotes as well.
    – JDB
    Commented Aug 22, 2014 at 13:20
13

It does look like you asked "by the book". There are a few things that I personally would tweak1, but it does all the things we suggest in How do I ask a good question?

So now you need to look at the comments:

It is very likely that this isn't a language based on a Chomsky type-3 grammar that can be parsed with regular expressions. Is the comma in "xx[xx","yy]yy" fit to split, or is it enclosed in brackets and should be ignored?

I admit, I had to look up Chomsky hierarchy to understand the comment. But the point of the comment is that regular expressions are designed to parse regular languages with very restricted rules. In order to answer the general case2, you would need to explain how the language you are parsing works down to the gritty details.

Do you plan to support nested braces of arbitrary depth??? If so: this is not possible to do with regular expressions. You'll need something that is at least as powerful as a pushdown automaton (regexes are only as powerful as a finite state machine). It's easy to implement this using a LIFO queue.

Again, this comment suggests that regular expressions might not be the proper tool for the job. There are a couple of links that you might want to follow in order to understand what the commenter is asking. But the upshot is that you might not be able to solve this problem with a Java regex if you want to parse strings with deep nesting braces.

I think that brackets in this particular case are ignored. But the potential problem is with nested json object, since the Java regex engine doesn't have a recursion feature. However, do not make confusion between regular expressions in the theoretical sense and what is commonly called 'regex' abusively (that is a tool with more advanced features and with less limitations, several regex engines have the recursion feature) otherwise you will experience an encounter of the third kind and they will take you directly to the planet Chomsky for applying the sentence.

This is technically a reply to the first comment, but you should note it as well. It points out that most regex engines are more powerful than the theoretical regular expression concept. In particular, some have a recursion feature that might be very helpful for solving the problem you pose. But Java's regex engine lacks that feature.

Finally, you got an answer that you have accepted3. The regex provided in the answer does not seem to cover the general case (for certain definitions of "general"). And I think that's where most of your downvotes are coming from. There is something very off-putting about the paragraph you did not mention in your meta post:

So teach me with this example. What is the regex to split by , ignoring the delimiter if it is between "" OR [] OR {}? Please make it as general as possible so I can use it anywhere and explain its syntax.

I've updated your question and included a the extra tests you put in the comments to the accepted answer. I can't guarantee that people will upvote it, but I think it's a better question. (And one I can upvote myself.)


  1. The title, for instance, is more convoluted than I'd like.

  2. By the way, when talking about making an answer as general as possible, that should be an indication that you should not be using a regex, but a general purpose language.

  3. Your comments on that answer come off as confrontational to me:

    Is this regex specific for my example? Because "ke,y":{"key,2":"val,ue"},"te,st":"h,i" returns "ke,y":{"key,2":"val,ue"} instead of "ke,y":{"key,2":"val,ue"} and "te,st":"h,i". I would a generic answer.

    and:

    System.out.printf("%s%n", tok): what does %s%n mean? Do I need to format every tok?

    I think it's the bolded bits that put them over the top.

5
  • That question has abundant usage of backticks for non-code, could have been fixed while you were editing it. Commented Aug 19, 2014 at 16:42
  • 1
    @InfiniteRecursion: I think it perfectly acceptable to use backticks to quote the delimiter and the quote/braces in this question. These are, in a sense, code. I would have likely added them if they hadn't existed.
    – Jon Ericson Staff
    Commented Aug 19, 2014 at 16:57
  • Thanks for sharing that perspective Jon. Now I am able to understand why those backticks are used. Commented Aug 19, 2014 at 17:01
  • I am not from US/UK so I don't know english very well. The Help Center is medium level and (really!) well-written, but other-sites tutorials are unreadable for me. In addition to, I am only 15 so my knowledge is low and I prefer String.split now. About your answer, what does it mean "asking by the book"? And can you explain note 3? Also, I wrote a title as specific as possible, as said on the Help Center.
    – user3453226
    Commented Aug 19, 2014 at 19:19
  • 4
    @Joiner: Your English is pretty good to my reading. I'm using "the book" as an idiom. I mean you did all the things we recommend. As for footnote 3: using bold for emphasis can sometimes seem like shouting would in real life. It's purpose is to make sure people are paying attention. So when you bolded "generic" is sounds like you assume the answerer didn't read the question. The second comment reemphasized the Listen up! tone. I'd go easy on the bold if you don't want to seem bold.;)
    – Jon Ericson Staff
    Commented Aug 19, 2014 at 19:28

You must log in to answer this question.