有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当存在未知数量的空格时,使用java替代正向查找

我的replacerRegex

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

获取正确的值,即valueToBePickedenter image description here

但是,我如何使("schedulingCancelModal": \{\s*? "title": ")不被包括在结果中,就像正向查找一样

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

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

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

共 (1) 个答案

  1. # 1 楼答案

    您只需选择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