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

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -