简体   繁体   English

代码中的Java修复正则表达式

[英]Java fix regex in code

I need to print @OPOK , but in the following code: 我需要打印@OPOK ,但是在以下代码中:

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 @OPOK","MSG3":"XXXXXX instead, how do I fix my pattern ? 我收到@OPOK”,“ MSG3”:“ XXXXXX” ,如何修复我的图案?

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 . 编辑添加 :如评论中@ bmorris591所建议,您可以添加一个额外的+ (如下所示)以使量词所有格 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. 有关更多详细信息,请参见Pattern文档。

Or of course you could just match against "any character other than a double quote" which is what Brian's approach will do. 或者,当然,您可以将其与“除了双引号之外的任何字符”匹配,这就是Brian的方法所要做的。 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. 它们之间可能存在性能差异(老实说,我希望Brian的性能更好),但是如果性能对您很重要,则应该测试两种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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