有 Java 编程相关的问题?

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

java如何匹配多个捕获组,但结果不符合预期

我正在努力学习Java正则表达式。我想将几个捕获组(即j(a(va)))与另一个字符串(即this is java. this is ava, this is va)进行匹配。我希望输出为:

I found the text "java" starting at index 8 and ending at index 12.
I found the text "ava" starting at index 21 and ending at index 24.    
I found the text "va" starting at index 34 and ending at index 36.
Number of group: 2

但是,IDE仅输出:

I found the text "java" starting at index 8 and ending at index 12.
Number of group: 2

为什么会这样?我有什么遗漏吗

原始代码:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("\nEnter your regex:");

        Pattern pattern
                = Pattern.compile(br.readLine());

        System.out.println("\nEnter input string to search:");
        Matcher matcher
                = pattern.matcher(br.readLine());

        boolean found = false;
        while (matcher.find()) {
            System.out.format("I found the text"
                    + " \"%s\" starting at "
                    + "index %d and ending at index %d.%n",
                    matcher.group(),
                    matcher.start(),
                    matcher.end());
            found = true;
            System.out.println("Number of group: " + matcher.groupCount());
        }
        if (!found) {
            System.out.println("No match found.");
        }

运行上述代码后,我输入了以下输入:

Enter your regex:
j(a(va))

Enter input string to search:
this is java. this is ava, this is va

和IDE输出:

I found the text "java" starting at index 8 and ending at index 12.
Number of group: 2

共 (2) 个答案

  1. # 1 楼答案

    您的regexp只匹配整个字符串java,它不匹配avava。当它匹配java时,它会将捕获组1设置为ava,将捕获组2设置为va,但它本身并不匹配这些字符串。生成所需结果的regexp是:

    j?(a?(va))
    

    ?使前面的项成为可选项,因此它将匹配后面没有这些前缀的项

    DEMO

  2. # 2 楼答案

    您需要正则表达式(j?(a?(va)))

    Pattern p = Pattern.compile("(j?(a?(va)))");
    Matcher m = p.matcher("this is java. this is ava, this is va");
    
    while( m.find() )
    {
        String group = m.group();
        int start = m.start();
        int end = m.end();
        System.out.format("I found the text"
                      + " \"%s\" starting at "
                     + "index %d and ending at index %d.%n",
                      group,
                      start,
                       end);
    
    
    
    }
    

    您可以看到演示here