简体   繁体   English

Java正则表达式返回完整字符串而不是捕获

[英]Java regex returns full string instead of capture

Java Code: Java代码:

String imagesArrayResponse = xmlNode.getChildText("files");
Matcher m = Pattern.compile("path\":\"([^\"]*)").matcher(imagesArrayResponse);

while (m.find()) {
    String path = m.group(0);
}

String: 串:

[{"path":"upload\/files\/56727570aaa08922_0.png","dir":"files","name":"56727570aaa08922_0","original_name":"56727570aaa08922_0.png"}{"path":"upload\/files\/56727570aaa08922_0.png","dir":"files","name":"56727570aaa08922_0","original_name":"56727570aaa08922_0.png"}{"path":"upload\/files\/56727570aaa08922_0.png","dir":"files","name":"56727570aaa08922_0","original_name":"56727570aaa08922_0.png"}{"path":"upload\/files\/56727570aaa08922_0.png","dir":"files","name":"56727570aaa08922_0","original_name":"56727570aaa08922_0.png"}]

m.group returns m.group退货

path":"upload\/files\/56727570aaa08922_0.png"

instead of captured value of path. 而不是路径的捕获​​值。 Where I am wrong? 我哪里错了?

See the documentation of group( int index ) method 请参阅group( int index )方法的文档

When called with 0, it returns the entire string. 用0调用时,它将返回整个字符串。 Group 1 is the first. 第一组是第一个。

To avoid such a trap, you should use named group with syntax : "path\\":\\"(?<mynamegroup>[^\\"]*)" 为避免此类陷阱,您应该使用命名组,其语法为: "path\\":\\"(?<mynamegroup>[^\\"]*)"

javadoc : javadoc

Capturing groups are indexed from left to right, starting at one. 捕获组从左到右从一个索引开始。 Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group(). 组零表示整个模式,因此表达式m.group(0)等效于m.group()。

m.group(1) will give you the Match. m.group(1)将为您提供匹配。 If there are more than one matchset (), it will be m.group(2), m.group(3),... 如果有多个Matchset(),则分别是m.group(2),m.group(3),...

By convention, AFAIK in regex engines the 0th group is always the whole matched string. 按照约定,正则表达式引擎中的AFAIK第0组始终是整个匹配的字符串。 Nested groups start at 1. 嵌套组从1开始。

Check out the grouping options in Matcher . Matcher中查看分组选项。

 Matcher m = 
   Pattern.compile(
    //<-     (0)     ->  that's group(0)
    //          <-(1)->  that's group(1)
     "path\":\"([^\"]*)").matcher(imagesArrayResponse);

Change your code to 将您的代码更改为

while (m.find()) {
  String path = m.group(1);
}

And you should be okay. 而且你应该没事。 This is also worth checking out: What is a non-capturing group? 这也值得一看: 什么是非捕获组? What does a question mark followed by a colon (?:) mean? 问号后跟冒号(?:)是什么意思?

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

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