Ask Experts Questions for FREE Help !
Ask
    preethyb's Avatar
    preethyb Posts: 1, Reputation: 1
    New Member
     
    #1

    Feb 17, 2015, 05:38 AM
    Split a string with comma neglecting commas inside special characters
    For this given input


    dog(dog1,dog2,dog3),cat{cat1,cat2,cat3},pe^t"pet1, pet2,pet3",pi*g[pig1,pig2,pig3]


    I want the output as
    dog(dog1,dog2,dog3)
    cat{cat1,cat2,cat3}
    pe^t"pet1,pet2,pet3"
    pi*g[pig1,pig2,pig3]


    I tried the following


    Code:
    String str = "dog(dog1,dog2),cat{cat1,cat2},pe^t\"pet1,pet2\",pi*g[pig1,pig2]";
    if (str.contains(",")) {
       String arry[] = str.split(",(? \\w+[})\\]\\\"])");
       int len = arry.length;
       for (int i = 0; i < len; i++) {
          if(arry[i].startsWith("\""))
          {
             System.err.println(arry[i].replaceAll("^\"|\"$", "").trim());
          }
          else{
             System.err.println(arry[i].trim());
          }
       }
    }

    But it will work only for when there are two strings between specialcharacters like the following
    "dog(dog1,dog2),cat{cat1,cat2},pe^t\"pet1,pet2\",p i*g[pig1,pig2]"

Check out some similar questions!

Does my sentence need a comma or commas? [ 1 Answers ]

Rules for commas, do I need to use one or not?

To extract characters appearing right side of a particular string [ 2 Answers ]

Mysql or excel function to extract characters after the occurrence of a particular string For example: Suppose "http://10.20.30.40:8080/aptv-web/PROGRAMS/TIMESNOW/LIVE/64BIT/TIMESNOWLive_03 " This is the statement through which I need to extract the characters appearing right side of...

Wondergirl, How Does a Person Know When to Add a Comma or Commas to a Sentence? [ 10 Answers ]

Hi, Wondergirl, or to Others Who Might Also Have the Answer! How does a person know when it's appropriate to add a comma or commas to a sentence, please? When and where... I tend to go "comma crazy" when I add them! I think that I add them too much. But, I'm not sure about that... ...

Adding special characters [ 3 Answers ]

I need to add the special characters for typing in French and Spanish in Microsoft Works. How do I add these special accented letters?


View more questions Search
 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.