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.