简体   繁体   中英

Java fix regex in code

I need to print , but in the following code: ,但是在以下代码中:

String s = "\"MSG1\":\"00\",\"MSG2\":\"@OPOK\",\"MSG3\":\"XXXXXX\"}";

Pattern pattern = Pattern.compile(".*\"MSG2\":\"(.+)\".*");
Matcher matcher = pattern.matcher(s);

if (matcher.find()) {
    System.out.println(matcher.group(1));
} else {
    System.out.println("Match not found");
}

I get instead, how do I fix my pattern ? ,如何修复我的图案?

You probably want the following:

Pattern pattern = Pattern.compile("\"MSG2\":\"([^\"]+)\"");

For the capture group you are interested in, this will match any character except a double quote. Since the group is surrounded by double quotes, this should prevent it from going "too far" in the match.

Edited to add : As @bmorris591 suggested in the comments, you can add an extra + (as shown below) to make the quantifier possessive . This may help improve performance in cases where the matcher fails to find a match.

Pattern pattern = Pattern.compile("\"MSG2\":\"([^\"]++)\"");

You want to make your .+ part reluctant . By default it's greedy - it'll match as much as it can without preventing the pattern from matching. You want it to match as little as it can, like this:

Pattern pattern = Pattern.compile(".*\"MSG2\":\"(.+?)\".*");

The ? is what makes it reluctant. See the Pattern documentation for more details.

Or of course you could just match against "any character other than a double quote" which is what Brian's approach will do. Both will work equally well as far as I'm aware; there may well be performance differences between them (I'd expect Brian's to perform better to be honest) but if performance is important to you you should test both approaches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM