模式匹配器返回意外值

你能看一下下面结果中的模式(java)吗?我预计结果应该给我 3 个组值,但相反它给了我 2 个值,只是我认为我错过了一些东西。


package com.mycompany.testapp;


import java.util.regex.Matcher;

import java.util.regex.Pattern;


/**

 *

 * @author bo17a

 */

public class NewClass {

    public static void main(String[] args){

        Pattern mPattern = Pattern.compile("^love (.*?) way you (.*?)$");

        Matcher matcher = mPattern.matcher("love the way you lie");

        if(matcher.find()){

            String[] match_groups = new String[matcher.groupCount()];

            System.out.println(String.format("groupCount: %d", matcher.groupCount()));

            for(int j = 0;j<matcher.groupCount();j++){

                System.out.println(String.format("j %d",j));

                match_groups[j] = matcher.group(j);

                System.out.println(match_groups[j]);

            }

        }


    }

}

我得到的结果是:


2

love the way you lie

the

但我的预期结果应该是:


3

love the way you lie

the

lie

更新

我尝试按照回复中的建议添加一组号码:


/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.

 */

package com.mycompany.testapp;


import java.util.regex.Matcher;

import java.util.regex.Pattern;


/**

 *

 * @author lee

 */

public class NewClass {

    public static void main(String[] args){

        Pattern mPattern = Pattern.compile("^love (.*?) way you (.*?)$");

        Matcher matcher = mPattern.matcher("love the way you lie");

        if(matcher.find()){

            String[] match_groups = new String[matcher.groupCount()];

                System.out.println(String.format("groupCount: %d", matcher.groupCount()));

                for(int j = 0;j<=matcher.groupCount();j++){

                    System.out.println(String.format("j %d",j));

                    match_groups[j] = matcher.group(j);

                    System.out.println(match_groups[j]);

                }

        }

    }


}


我已经在 Windows 10 JDK 8 Mac OS JDK 8 上尝试过。那么,这可能是 Java 中的一个错误,因为你和我对相同的代码有不同的结果?

回首忆惘然
浏览 87回答 2
2回答

狐的传说

我的猜测是,您还希望以团队形式捕捉完整比赛,^(love (.*?) way you (.*?))$或者直接在你的计数器上加 1:测试1import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegularExpression{&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; Pattern mPattern = Pattern.compile("^love (.*?) way you (.*?)$");&nbsp; &nbsp; &nbsp; &nbsp; Matcher matcher = mPattern.matcher("love the way you lie");&nbsp; &nbsp; &nbsp; &nbsp; if(matcher.find()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] match_groups = new String[matcher.groupCount() + 1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.format("groupCount: %d", matcher.groupCount() + 1));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j = 0;j<matcher.groupCount() + 1;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.format("j %d",j));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match_groups[j] = matcher.group(j);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(match_groups[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出groupCount: 3j 0love the way you liej 1thej 2lie测试2import java.util.regex.Matcher;import java.util.regex.Pattern;public class RegularExpression{&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; final String regex = "^(love (.*?) way you (.*?))$";&nbsp; &nbsp; &nbsp; &nbsp; final String string = "love the way you lie";&nbsp; &nbsp; &nbsp; &nbsp; final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);&nbsp; &nbsp; &nbsp; &nbsp; final Matcher matcher = pattern.matcher(string);&nbsp; &nbsp; &nbsp; &nbsp; while (matcher.find()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Full match: " + matcher.group(0));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 1; i <= matcher.groupCount(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Group " + i + ": " + matcher.group(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出Full match: love the way you lieGroup 1: love the way you lieGroup 2: theGroup 3: lie正则表达式电路jex.im可视化正则表达式:

九州编程

显然,matcher.groupCount()在您的情况下返回 2,因此您只需构造两个字符串的数组并将数字小于 2 的组复制到其中,即组 0(整个字符串)和组 1(“the”)。如果您matcher.groupCount()在整个代码中添加 1,它将按预期工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java