Java Regex捕获组 [英] Java Regex Capturing Groups

查看:167
本文介绍了Java Regex捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解这个代码块。在第一个中,我们在表达式中寻找的是什么?

I am trying to understand this code block. In the first one, what is it we are looking for in the expression?

我的理解是它是任何字符(0或更多次*)后跟任何数字介于0和9之间(一次或多次+)后跟任何字符(0次或多次*)。

My understanding is that it is any character (0 or more times *) followed by any number between 0 and 9 (one or more times +) followed by any character (0 or more times *).

执行此操作时,结果为:

When this is executed the result is:

Found value: This order was placed for QT3000! OK?
Found value: This order was placed for QT300
Found value: 0

有人可以和我一起讨论吗?

Could someone please go through this with me?

使用捕获组有什么好处?

What is the advantage of using Capturing groups?

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTut3 {

    public static void main(String args[]) {
        String line = "This order was placed for QT3000! OK?"; 
        String pattern = "(.*)(\\d+)(.*)";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(line);

        if (m.find()) {
            System.out.println("Found value: " + m.group(0));
            System.out.println("Found value: " + m.group(1));
            System.out.println("Found value: " + m.group(2));
        } else {
            System.out.println("NO MATCH");
        }
    }

}


推荐答案

您遇到的问题是量词的类型。您在第一组中使用贪心量词(索引 1 - 索引0代表整个模式),这意味着它将尽可能多地匹配(因为它是任何字符,它将匹配尽可能多的字符以满足条件下一组)。

The issue you're having is with the type of quantifier. You're using a greedy quantifier in your first group (index 1 - index 0 represents the whole Pattern), which means it'll match as much as it can (and since it's any character, it'll match as many characters as there are in order to fulfill the condition for the next groups).

简而言之,只要下一组,您的第一组。* 就会匹配任何内容\\d + 可以匹配某些东西(在这种情况下,最后一位数字)。

In short, your 1st group .* matches anything as long as the next group \\d+ can match something (in this case, the last digit).

根据第3组,它将匹配最后一位数后的任何内容。

As per the 3rd group, it will match anything after the last digit.

如果您将其更改为第一组中的不情愿量词,您将得到我认为您期望的结果,即 3000 部分。

If you change it to a reluctant quantifier in your 1st group, you'll get the result I suppose you are expecting, that is, the 3000 part.

注意第一组中的问号

String line = "This order was placed for QT3000! OK?";
Pattern pattern = Pattern.compile("(.*?)(\\d+)(.*)");
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
    System.out.println("group 1: " + matcher.group(1));
    System.out.println("group 2: " + matcher.group(2));
    System.out.println("group 3: " + matcher.group(3));
}

输出:

group 1: This order was placed for QT
group 2: 3000
group 3: ! OK?

关于Java的更多信息模式 这里

More info on Java Pattern here.

最后,捕获组由圆括号分隔,并提供一种非常有用的方法来使用反向引用(除其他外),一旦你的 Pattern 与输入匹配。

Finally, the capturing groups are delimited by round brackets, and provide a very useful way to use back-references (amongst other things), once your Pattern is matched to the input.

在Java 6中,只能按顺序引用组(谨防嵌套组和排序的细微之处)。

In Java 6 groups can only be referenced by their order (beware of nested groups and the subtlety of ordering).

在Java 7中,它更容易,因为您可以使用命名组。

In Java 7 it's much easier, as you can use named groups.

这篇关于Java Regex捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆