我的replacerRegex

("schedulingCancelModal": \{\s*? "title": ")(.+?)(?=")


正确的值被获取,即valueToBePicked
java - 当空格数量未知时,替代正向后看-LMLPHP

但是,如何使("schedulingCancelModal": \{\s*? "title": ")不像正向后看那样包含在结果中?

到目前为止,我的Java代码:

Pattern replacerPattern = Pattern.compile(replacerRegex);
Matcher matcher = replacerPattern.matcher(value);

while (matcher.find()) {
    String valueToBePicked = matcher.group();
}

最佳答案

您只需选择matcher.group(2),它将为您提供第二个捕获组的内容。例如:

    String replacerRegex = "(\"schedulingCancelModal\": \\{\\s*? \"title\": \")(.+?)(?=\")";
    String value = "\"valueToBePicked\": \"schedulingCancelModal\": {\n \"title\": \"Are you sure you want to leave scheduling?\", ... }";
    Pattern replacerPattern = Pattern.compile(replacerRegex);
    Matcher matcher = replacerPattern.matcher(value);

    while (matcher.find()) {
        String valueToBePicked = matcher.group(2);
        System.out.println(valueToBePicked);
    }


输出:

Are you sure you want to leave scheduling?


Demo on rextester

09-05 01:17