繁体   English   中英

Beanshell 模式匹配器正则表达式

[英]Beanshell Pattern Matcher Regex

我有以下 JSON:

[
    {
        "": "",
        "substituted_restday": "2020-02-01",
        "original_restday": "2020-02-08",
        "id": "15d13f70-c0a852c0-3a925f13-6dca1982",
        "_UNIQUEKEY_": "15d1592a-c0a852c0-3a925f13b7c65023",
        "parentId": ""
    }, 
    {
        "": "",
        "substituted_restday": "2020-02-03",
        "original_restday": "2020-02-09",
        "id": "15d14d55-c0a852c0-3a925f13-727b70af",
        "_UNIQUEKEY_": "15d1592a-c0a852c0-3a925f13-3711a584",
        "parentId": ""
    }
]

我想从“替换休息日”中提取“日期”,即 2020-02-01 和 2020-02-03。

我有以下代码,但结果不好:

Pattern pattern = Pattern.compile("((\"substituted_restday\":\")[0-9-]+)");
Matcher matcher = pattern.matcher(JSON);
System.out.println(matcher.find());                     
if (matcher.find()){
    System.out.println(matcher.group(0));
    System.out.println(matcher);
    System.out.println("group count:" + matcher.groupCount());
    for (int i = 0; i <= matcher.groupCount(); i++) {
        System.out.println(i + ":" + matcher.group(i));
    }
}

我可以看到“matcher.find()”是真的。 但是 matcher.group 没有价值。 请帮忙。

您必须遍历匹配的模式,然后访问group(1)

public static void main(String[] args) {
    Pattern pattern = Pattern.compile("\"substituted_restday\": \"([0-9-]+)\"");
    Matcher matcher = pattern.matcher(JSON);
    while(matcher.find()){
        System.out.println(matcher.group(1));
    }
} 

输出将是:

2020-02-01
2020-02-03

如果您访问group(0)则输出为:

 "substituted_restday": "2020-02-01"
 "substituted_restday": "2020-02-03"

组由括号标识

group(0) :( ("\\"substituted_restday\\": \\"([0-9-]+)\\"")

group(1)([0-9-]+)

暂无
暂无

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

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