简体   繁体   English

Java正则表达式捕获数字组

[英]Java regular expression capture group of numbers

I'm having a heck of a time getting this regular expression to output the groups I expect: 我很想让这个正则表达式输出我期望的组:

Pattern p = Pattern.compile("(point) (.*)");
Matcher m = p.matcher("point 0 0 255 0 0");

if (m.find()) {
    for (int i = 0; i < m.groupCount(); i++) {
        System.out.println("-- " + m.group(i));
    }
}

I am hoping to see this output: 我希望看到以下输出:

point 0 0 255 0 0   //  this is group 0
point
0 0 255 0 0

Instead, I get group 0 and 1 - nothing from the last group. 取而代之的是,我得到的是第0组和第1组-最后一个组什么也没有。

I don't know what I'm doing wrong. 我不知道我在做什么错。

In the long run, I'm trying to match (point|rect|line) and a variable number of integers. 从长远来看,我试图匹配(point|rect|line)和可变数量的整数。

Thanks... 谢谢...

You are not printing all the groups. 您没有打印所有组。 Try for (int i = 0; i <= m.groupCount(); i++) 尝试for (int i = 0; i <= m.groupCount(); i++)

Group 0 always refers to the originally matched string - in your example, point 0 0 255 0 0 . 0始终引用原始匹配的字符串-在您的示例中, point 0 0 255 0 0 Therefore, the numbered capture groups in your pattern are referred to by a 1-based index - In your example group 1 , group 2 . 因此,模式中编号的捕获组由基于1的索引引用-在示例组1 ,组2

This is a common mannerism with other programming languages - JavaScript and Perl also denote the index-zero-match (also referred to as \\0 in "replace" functions) as the whole matched string. 这是其他编程语言的常见习惯用法-JavaScript和Perl还将索引零匹配(在“替换”函数中也称为\\0 )表示为整个匹配字符串。

Therefore, to output your expected capture groups, you will need to loop from 0 to 2 or from 1 to 2: 因此,要输出预期的捕获组,您将需要从0循环到2或从1循环到2:

for (int i = 0; i <= m.groupCount(); i++) {
    //             ^--  changed "<" to "<="
    System.out.println("-- " + m.group(i));
}

Output:
point 0 0 255 0 0
point
0 0 255 0 0

Have a look at the MatchResult reference on https://docs.oracle.com/javase/7/docs/api/java/util/regex/MatchResult.html and check 'groupCount' section. https://docs.oracle.com/javase/7/docs/api/java/util/regex/MatchResult.html上查看MatchResult参考,然后选中“ groupCount”部分。 It is said that group zero denotes the entire pattern by convention and not included in this count. 据说零组按照惯例表示整个模式,不包括在此计数中。

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

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