Java 7 中的正则表达式命名捕获组支持

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27498106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:53:31  来源:igfitidea点击:

Regular Expression named capturing groups support in Java 7

javaregexjava-7

提问by Rafael Borja

Since Java 7 regular expressions API offers support for named capturing groups. The method java.util.regex.Matcher.group(String)returns the input subsequence captured by the given named-capturing group, but there's no example available on API documentations.

由于 Java 7 正则表达式 API 提供了对命名捕获组的支持。方法java.util.regex.Matcher.group(String)返回由给定命名捕获组捕获的输入子序列,但 API 文档中没有可用的示例。

What is the right syntax to specify and retrieve a named capturing group in Java 7?

在 Java 7 中指定和检索命名捕获组的正确语法是什么?

回答by nhahtdh

Specifying named capturing group

指定命名的捕获组

Use the following regex with a single capturing group as an example ([Pp]attern).

将以下正则表达式与单个捕获组一起用作示例([Pp]attern)

Below are 4 examples on how to specify a named capturing groupfor the regex above:

以下是有关如何为上述正则表达式指定命名捕获组的4 个示例:

(?<Name>[Pp]attern)
(?<group1>[Pp]attern)
(?<name>[Pp]attern)
(?<NAME>[Pp]attern)

Note that the name of the capturing groupmust strictly matches the following Pattern:

请注意,捕获组名称必须严格匹配以下 Pattern:

[A-Za-z][A-Za-z0-9]*

The group name is case-sensitive, so you must specify the exact group name when you are referring to them (see below).

组名区分大小写,因此您在引用它们时必须指定确切的组名(见下文)。

Backreference the named capturing group in regex

在正则表达式中反向引用命名的捕获组

To back-reference the content matched by a named capturing groupin the regex (correspond to 4 examples above):

要在正则表达式中反向引用与命名捕获组匹配的内容(对应于上面的 4 个示例):

\k<Name>
\k<group1>
\k<name>
\k<NAME>

The named capturing group is still numbered, so in all 4 examples, it can be back-referenced with \1as per normal.

命名的捕获组仍然编号,因此在所有 4 个示例中,它可以\1按正常方式进行反向引用。

Refer to named capturing group in replacement string

参考替换字符串中的命名捕获组

To refer to the capturing group in replacement string(correspond to 4 examples above):

在替换字符串中引用捕获组(对应于上面的 4 个示例):

${Name}
${group1}
${name}
${NAME}

Same as above, in all 4 examples, the content of the capturing group can be referred to with $1in the replacement string.

同上,在所有4个例子中,捕获组的内容都可以$1在替换字符串中引用。

Named capturing group in COMMENTmode

COMMENT模式下的命名捕获组

Using (?<name>[Pp]attern)as an example for this section.

使用(?<name>[Pp]attern)作为该部分的示例。

Oracle's implementation of the COMMENTmode (embedded flag (?x)) parses the following examples to be identical to the regex above:

Oracle 对COMMENT模式(嵌入标志(?x))的实现将以下示例解析为与上面的正则表达式相同:

(?x)  (  ?<name>             [Pp] attern  )
(?x)  (  ?<  name  >         [Pp] attern  )
(?x)  (  ?<  n  a m    e  >  [Pp] attern  )

Except for ?<which must notbe separated, it allows arbitrary spacing even in between the name of the capturing group.

除了?<必须被分离,它允许任意间距即使在捕获组的名称之间。

Same name for different capturing groups?

不同捕获组的名称相同?

While it is possible in .NET, Perl and PCRE to define the same name for different capturing groups, it is currently not supportedin Java (Java 8). You can't use the same name for different capturing groups.

虽然在 .NET、Perl 和 PCRE 中可以为不同的捕获组定义相同的名称,但目前在 Java (Java 8) 中不受支持。您不能对不同的捕获组使用相同的名称。

Named capturing group related APIs

命名捕获组相关 API

New methods in Matcher class to support retrieving captured text by group name:

Matcher 类中的新方法支持按组名检索捕获的文本:

The corresponding method is missing from MatchResultclass as of Java 8. There is an on-going Enhancement request JDK-8065554for this issue.

MatchResultJava 8 开始,类中缺少相应的方法。针对此问题,有一个持续的增强请求 JDK-8065554

There is currently no API to get the list of named capturing groups in the regex. We have to jump through extra hoops to get it. Though it is quite useless for most purposes, except for writing a regex tester.

目前没有 API 来获取正则表达式中命名捕获组的列表。我们必须跳过额外的箍才能得到它。尽管除了编写正则表达式测试器之外,它对于大多数用途来说毫无用处。

回答by Rafael Borja

The new syntax for a named capturing group is (?<name>X)for a matching group X named by "name". The following code captures the regex (\w+) (any group of alphanumeric characters). To name this capturing group you must add the expression ? inside the parentheses just before the regex to be captured.

命名捕获组的新语法适用于以(?<name>X)“name”命名的匹配组 X。以下代码捕获正则表达式 (\w+)(任何字母数字字符组)。要命名此捕获组,您必须添加表达式 ? 在要捕获的正则表达式之前的括号内。

Pattern compile = Pattern.compile("(?<teste>\w+)");
Matcher matcher = compile.matcher("The first word is a match");
matcher.find();
String myNamedGroup= matcher.group("teste");
System.out.printf("This is yout named group: %s", myNamedGroup);

This code returns prints the following output:

此代码返回打印以下输出:

This is your named group: The

这是您命名的组:The