java - Defining two same group names in regex -
string strr = "{msg=1234,returncode=123}"; pattern pat1 = pattern.compile("^\\{returncode=(?<returncode>\\d+)(,msg=(?<msg>.*))?\\}|(\\{msg=(?<msg2>.*),returncode=(?<returncode2>.+)\\})?$");
in fact, want define returncode in regex, compiler throws:
named capturing group defined.
how can solve it? maybe there easier way it. much
string strr = "{msg=1234,returncode=123}"; pattern pat2 = pattern.compile("(?<key>\\w+)=(?<val>\\w+)"); matcher m = pat2.matcher(strr); while (m.find()) { if (m.group("key").equals("returncode")) { system.out.println(m.group("val")); // prints 123 } }
Comments
Post a Comment