java 命名捕获组,在Java 7中,正则表达式命名为捕获组支持

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.

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

解决方案

Specifying named capturing group

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

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

(?[Pp]attern)

(?[Pp]attern)

(?[Pp]attern)

(?[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).

Backreference the named capturing group in regex

\k

\k

\k

\k

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

Refer to named capturing group in replacement string

${Name}

${group1}

${name}

${NAME}

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

Named capturing group in COMMENT mode

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

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

(?x) ( ? [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.

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 supported in Java (Java 8). You can't use the same name for different capturing groups.

Named capturing group related APIs

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

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值