试图了解“捕获组”在Java正则表达式中 [英] Trying to understand "Capturing groups" in regex with Java

查看:88
本文介绍了试图了解“捕获组”在Java正则表达式中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Java OCP,此刻我一直不了解捕获组 部分。作为描述,它太抽象了。

I am studying for the java OCP and at the moment I am stuck at understanding the "Capturing groups" section. It is a way too abstract as a description. Could you please (if you have time) give me some real examples using "Capturing groups"?

有人能为我提供以下陈述的具体例子吗?

Is anybody able to provide me with a concrete example of the following statement?


捕获组是将多个字符视为单个
单元的一种方式。通过将要分组的字符放置在
中的一组括号中来创建它们。例如,正则表达式(狗)
创建一个包含字母 d, o和 g的单个组。输入字符串中与捕获组匹配的
部分将被保存在内存中的
,以便以后通过反向引用进行调用(如下面反向引用一节中讨论的
一样)。

Capturing groups are a way to treat multiple characters as a single unit. They are created by placing the characters to be grouped inside a set of parentheses. For example, the regular expression (dog) creates a single group containing the letters "d" "o" and "g". The portion of the input string that matches the capturing group will be saved in memory for later recall via backreferences (as discussed below in the section, Backreferences).

我敢肯定,只要看到一个具体的例子,我就会尽快得到它。

I am pretty sure I'll get it as soon as I see a concrete example.

推荐答案

除其他事项外,regex允许您获取输入中与常规部分不同的部分表达。有时您需要整个比赛,但通常只需要其中一部分。例如,此正则表达式匹配 Y的第X页 字符串:

Among other things, regex lets you obtain portions of the input that were matched by various parts of the regular expression. Sometimes you need the entire match, but often you need only a part of it. For example, this regular expression matches "Page X of Y" strings:

Page \d+ of \d+

如果您将其传递为字符串

If you pass it a string

Page 14 of 203

您将匹配整个字符串。现在,假设您只需要 14 203 。没问题-正则表达式库使您可以将两个 \d + 括在括号中,然后仅检索 14 203 字符串。

you will match the entire string. Now let's say that you want only 14 and 203. No problem - regex library lets you enclose the two \d+ in parentheses, and then retrieve only the "14" and "203" strings from the match.

Page (\d+) of (\d+)

上面的表达式创建两个捕获组。通过匹配模式获得的 Matcher 对象使您可以分别检索这些组的内容:

The above expression creates two capturing groups. The Matcher object obtained by matching the pattern lets you retrieve the content of these groups individually:

Pattern p = Pattern.compile("Page (\\d+) of (\\d+)");
String text = "Page 14 of 203";
Matcher m = p.matcher(text);
if (m.find()) {
    System.out.println(m.group(1));
    System.out.println(m.group(2));
}

此打印 14 203

ideone上的演示

这篇关于试图了解“捕获组”在Java正则表达式中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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