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

时间:2015-12-17 10:07:28

标签: java regex

Java代码:

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

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

字符串:

[{"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返回

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

而不是捕获的路径值。哪里错了?

4 个答案:

答案 0 :(得分:5)

请参阅group( int index )方法

的文档

当使用0调用时,它返回整个字符串。第1组是第一组。

要避免此类陷阱,您应该使用带语法的命名组: "path\":\"(?<mynamegroup>[^\"]*)"

javadoc

  

捕获组从左到右编制索引,从1开始。组零表示整个模式,因此表达式m.group(0)等同于m.group()。

答案 1 :(得分:1)

m.group(1)会给你比赛。如果有多个matchset(),它将是m.group(2),m.group(3),...

答案 2 :(得分:0)

按照惯例,正则表达式引擎中的AFAIK第0组始终是整个匹配的字符串。嵌套组从1开始。

答案 3 :(得分:0)

查看Matcher中的分组选项。

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

将您的代码更改为

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

你应该没问题。这也值得一试:What is a non-capturing group? What does a question mark followed by a colon (?:) mean?