Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.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_String_Matcher - Fatal编程技术网

Java 多次在两个字符串之间使用正则表达式

Java 多次在两个字符串之间使用正则表达式,java,regex,string,matcher,Java,Regex,String,Matcher,我有一根像 [{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldP

我有一根像

[{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldPrice":"0","products":["609","613","617","621","624","628","632"]},{"id":"36","label":"GG","price":"0","oldPrice":"0","products":["608","612","616","620","623","627","631"]},{"id":"152","label":"XGG","price":"0","oldPrice":"0","products":["3713","6577","6578","6579","6580","6581","6582"]}]
我需要的是标签值:p,M,G,GG,XGG。我试着把标签和逗号之间的所有东西都取出来,但都不起作用

标签。*

这就行了

或者如果您真的想将其解析为json

这样做

var arr = [];
var jso = [{"id":"9","label":"P","price":"0","oldPrice":"0","products":["606","610","614","618","622","625","629"]},{"id":"8","label":"M","price":"0","oldPrice":"0","products":["607","611","615","619","626","630"]},{"id":"7","label":"G","price":"0","oldPrice":"0","products":["609","613","617","621","624","628","632"]},{"id":"36","label":"GG","price":"0","oldPrice":"0","products":["608","612","616","620","623","627","631"]},{"id":"152","label":"XGG","price":"0","oldPrice":"0","products":["3713","6577","6578","6579","6580","6581","6582"]}];
console.log(jso.length);
for(var i=0;i<jso.length;i++){
    arr.push(jso[i].label);
}
console.log(arr);

regex演示:

此字符串为JSON格式,因此建议使用JSON解析器,而不是使用regex

阅读中的JSON格式


此链接提供了有关java中JSON的说明

您的正则表达式是贪婪的

把它改成lazy

标签。*

还可以使用条件正则表达式仅获取标签值

使用条件表达式的正则表达式

但是,您可能应该使用JSON解析器

此正则表达式将捕获组名称标签中的所有标签

.*?(?:label)":"(?<labels>[^"]+)+.*?
或者,根据您执行此操作的方式,只需搜索此匹配项

(?:label)":"(?<labels>[^"]+)
您可以看到它在这里工作:

以及由regex101生成的结果JAVA:

String re = "(?:label)\\":\\"(?<labels>[^\\"]+)";
String str = "[{\"id\":\"9\",\"label\":\"P\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"606\",\"610\",\"614\",\"618\",\"622\",\"625\",\"629\"]},{\"id\":\"8\",\"label\":\"M\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"607\",\"611\",\"615\",\"619\",\"626\",\"630\"]},{\"id\":\"7\",\"label\":\"G\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"609\",\"613\",\"617\",\"621\",\"624\",\"628\",\"632\"]},{\"id\":\"36\",\"label\":\"GG\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"608\",\"612\",\"616\",\"620\",\"623\",\"627\",\"631\"]},{\"id\":\"152\",\"label\":\"XGG\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"3713\",\"6577\",\"6578\",\"6579\",\"6580\",\"6581\",\"6582\"]}]
";

Pattern p = Pattern.compile(re, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);

谢谢,我知道它是JSON格式的,但我不能使用解析器,它是来自web爬虫的字符串对象,我无法更改。您可以解析字符串中的JSON内容
String re = "(?:label)\\":\\"(?<labels>[^\\"]+)";
String str = "[{\"id\":\"9\",\"label\":\"P\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"606\",\"610\",\"614\",\"618\",\"622\",\"625\",\"629\"]},{\"id\":\"8\",\"label\":\"M\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"607\",\"611\",\"615\",\"619\",\"626\",\"630\"]},{\"id\":\"7\",\"label\":\"G\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"609\",\"613\",\"617\",\"621\",\"624\",\"628\",\"632\"]},{\"id\":\"36\",\"label\":\"GG\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"608\",\"612\",\"616\",\"620\",\"623\",\"627\",\"631\"]},{\"id\":\"152\",\"label\":\"XGG\",\"price\":\"0\",\"oldPrice\":\"0\",\"products\":[\"3713\",\"6577\",\"6578\",\"6579\",\"6580\",\"6581\",\"6582\"]}]
";

Pattern p = Pattern.compile(re, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(str);