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"
而不是路径的捕获值。我哪里错了?
请参阅group( int index )
方法文档
用0调用时,它将返回整个字符串。第一组是第一个。
为避免此类陷阱,应使用带语法的命名组:
"path\":\"(?
javadoc:
捕获组从左到右从一个索引开始。组零表示整个模式,因此表达式m.group(0)等效于m.group()。