1

I wanna parse a string like {"aaa","bbb", "ccc"} to aaa,bbb,ccc. How can I do it in regex in java? I've tried to write code as below:

String s = "{\"aaa\",\"bbb\", \"ccc\"}";
Pattern pattern = Pattern.compile("\\{\\\"([\\w]*)\\\"([\\s]*,[\\s]*\\\"([\\w]*)\\\")*}");
Matcher matcher = pattern.matcher(s);
if(matcher.find())      {
    StringBuilder sb = new StringBuilder();
    int cnt = matcher.groupCount();
    for(int i = 0; i < cnt; ++i)        {
        System.out.println(matcher.group(i));
    }
}

but the output is

{"aaa","bbb", "ccc"}
aaa
, "ccc"

I have a feeling that this is something about group and no-greedy match, but I don't know how to write it, could anyone help me? Thanks a lot!

P.S. I know how to do it with method like String.replace, I just wanna know could it be done by regex. Thanks

Thanks for all your answers and time, but what I want at first is a delegate solution using regex in java, especially group in regex. I want to know how to use group to solve it.

9
  • 2
    Is there a reason you can't use s = s.replace("\"", "") and then chop off the beginning and ending braces using s = s.substring(1, s.length() - 1) instead of a regex? Commented Mar 17, 2013 at 10:14
  • @merlin2011 then split by regex: , ? Commented Mar 17, 2013 at 10:16
  • @Judking, can you tell us a little more? Do your strings have escaped quotes or commas that we may not want to remove? Commented Mar 17, 2013 at 10:18
  • @ka, Yes, I misunderstood the question at first, but yes. Commented Mar 17, 2013 at 10:19
  • @Pragnani: this is not JSON. Commented Mar 17, 2013 at 10:30

3 Answers 3

1

RegEx ONLY matching: quite complex

RegEx Pattern: (?:\{(?=(?:"[^"]+"(?:, ?|(?=\})))*\})|(?!^)\G, ?)"([^"]+)"
Note: it needs the global modifier g, needs escaping, works with unlimited number of tokens

Explained demo here: http://regex101.com/r/iE9gS1

Sign up to request clarification or add additional context in comments.

Comments

1

Try this.

import java.util.*;
public class Main{

    public static void main(String[] args){
        String s = "{\"aaa\",\"bbb\", \"ccc\"}";
        s = s.substring(1,s.length() -1 );
        s = s.replace("\"","");
        String[] sa = s.split(", ?");
        for (int i = 0; i < sa.length; i++)
            System.out.println(sa[i]);
    }   
}

2 Comments

I know how to do it without using regex, could it be done using regex??Thx
better split("(?<=\"), ?(?=\")") first then replace the quotes! in case there are comma separated values inside the "quotes"
1

Try this sample code :

public class RegexTester {
public static void main(String[] args) throws Exception {
    String data = "{\"aaa\",\"bbb\", \"ccc\"}";

    String modifiedData = data.replaceAll("\\{|\"|\\}", "");

    System.out.println("Modified data : " + modifiedData);
}

Using regex try this pattarn :

Pattern pattern = Pattern.compile("\\{\"(.+?)\"(,?)\"(.+?)\"(,?)\\s*\"(.+?)\"");
    Matcher matcher = pattern.matcher(data);
    System.out.println("Started");
    while (matcher.find()) {

        System.out.print(matcher.group(1));
        System.out.print(matcher.group(2));
        System.out.print(matcher.group(3));
        System.out.print(matcher.group(4));
        System.out.print(matcher.group(5));
    }

Hope this addresses your issue.

3 Comments

I know how to do it without using regex, could it be done using regex??Thx
@Judking: ReplaceAll uses a regex as its search term, so this is using regular expressions
I have modified my answer and provided a sample regex pattern.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.