本文介绍了在Java 7中,正则表达式命名为捕获组支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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中指定和检索命名捕获组的正确语法是什么?

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

推荐答案

指定命名捕获组



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

以下是有关如何指定:

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

请注意必须严格匹配以下模式:

Note that the name of the capturing group must strictly matches the following 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).

要(对应于上面的4个示例):

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

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

指定的捕获组仍然编号,因此在所有4个示例中,它都可以反向引用按正常情况 \1

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

至(对应上面的4个示例):

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

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

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

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

使用(?< name> [ Pp] attern)作为前本部分内容充足。

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

Oracle执行 COMMENT 模式(嵌入式标志(? x))解析以下示例与上述正则表达式相同:

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

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

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

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

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

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

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

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


  • (来自Java 7)

  • (来自 Java 8

  • (来自 Java 8

  • group(String name) (from Java 7)
  • start(String name) (from Java 8)
  • end(String name) (from Java 8)

类。正在进行。

目前没有API可以获取命名列表捕获正则表达式中的组。 。虽然它在大多数情况下都没用,除了编写正则表达式测试器。

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.

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

10-29 19:48