Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java正则表达式返回完整字符串而不是捕获_Java_Regex - Fatal编程技术网

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

Java正则表达式返回完整字符串而不是捕获,java,regex,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.p

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、 团体回报

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

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

m.group(1)将为您提供匹配。如果有多个匹配集(),它将是m.group(2),m.group(3),…

m.group(1)将为您提供匹配。如果有多个匹配集(),它将是m.group(2)、m.group(3),…

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

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

请参阅
组(int index)
方法的文档

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

要避免此类陷阱,应使用语法为的命名组:
“路径\”:“(?[^\”]*)”

:

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


请参阅
group(int index)
方法的文档

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

要避免此类陷阱,应使用语法为的命名组:
“路径\”:“(?[^\”]*)”

:

捕获组从左到右索引,从一开始。组0表示整个模式,因此表达式m.Group(0)等价于m.Group()


查看中的分组选项


你应该没事的。这也值得检查:

检查中的分组选项

你应该没事的。这也值得一看:

path=m.group(1)
path=m.group(1)
 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);
}