简体   繁体   English

为什么我从正则表达式匹配器中得到这些结果?

[英]Why do I'm getting these results from my regex matcher?

I have this code below: 我在下面的代码:

    String content = "title = 123";
    Pattern p = Pattern.compile("(title)");
    Matcher m = p.matcher(content);

    int i = 1;
    while (m.find()) {
        System.out.println("groupCount() = " + m.groupCount());
        System.out.println("i = " + i++ + "     found: " + m.group(0));
        System.out.println("i = " + i++ + "     found: " + m.group(1));
    }

The output is: 输出为:

groupCount() = 1
i = 1   found: title
i = 2   found: title

Can someone please tell me why I have 1 group count and also if I get correct values in group(0) and group(1) ? 有人可以告诉我为什么我有1个组数以及在group(0)group(1)得到正确的值吗?

Are there any differences between group(0) and group(1) ? group(0)group(1)之间有什么区别吗?

Seed in the documentation: 种子在文档中:
groupCount() groupCount()

Group zero denotes the entire pattern by convention. 零组按照惯例表示整个模式。 It is not included in this count. 它不包括在此计数中。

and
group(int) 组(INT)

Capturing groups are indexed from left to right, starting at one. 捕获组从左到右从一个索引开始。 Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group(). 组零表示整个模式,因此表达式m.group(0)等效于m.group()。

So group 0 is the whole string that will be matched. 因此,组0是将被匹配的整个字符串。 and you have a capturing group with the whole string . 并且您有一个包含整个字符串的捕获组。 So there is technically 2 group. 因此,从技术上讲有2组。

0 is the default 默认为0
1 is your defined 1是您定义的

Defined group indexing start from 1 . 定义的组索引从1开始。

Why I have 1 group count? 为什么我有1组人数?

See Matcher specs : 请参阅Matcher规格

groupCount() - Returns the number of capturing groups in this matcher's pattern. groupCount() -返回此匹配器模式中捕获组的数量。

You only have 1 capturing group: (title) . 您只有1个捕获组: (title)

Are there any differences between group(0) and group(1) ? group(0)和group(1)之间有什么区别吗?

group(0) is the whole match (ie the string that was matched with the whole pattern), group(1) is only the substring captured with the first numbered capturing group. group(0)是整个匹配项(即与整个模式匹配的字符串), group(1)只是第一个编号的捕获组捕获的子字符串。 In your particular case, group(0) and group(1) are equal, because you set a capturing group to the whole pattern. 在您的特定情况下, group(0)group(1)相等,因为您将捕获组设置为整个模式。 Thus, the whole match (in group(0) ) and the captured substring (in group(1) ) coincide. 因此,整个匹配项(在group(0) )和捕获的子字符串(在group(1) )是重合的。

Best practice is not use a capturing group around the whole pattern since you always have access to it using group(0) . 最佳实践是不要在整个模式周围使用捕获组,因为您始终可以使用group(0)对其进行访问。

group 0 is the entire string which is being used. group 0是正在使用的整个字符串。 Since you are capturing everything, group 0 and group 1 (which represents the first captured / matched group) will be same. 由于您正在捕获所有内容,因此group 0group 1 (代表第一个捕获/匹配的组)将相​​同。 If you had something like this : 如果您有这样的事情:

Pattern p = Pattern.compile("(title) = 123"); then you will be able to find the difference between groups 0 and 1. 那么您将能够找到0组和1组之间的差异。

Note : group() is same as group(0) 注意: group()group(0)相同

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

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