开发者

Identifying capture groups in a Regex Pattern

开发者 https://www.devze.com 2023-02-02 01:24 出处:网络
Is there a way in Java (perhaps with an additional Open Source library) to identify the capture groups in a java.util.regex.Pattern (i.e. before creating a Matcher)

Is there a way in Java (perhaps with an additional Open Source library) to identify the capture groups in a java.util.regex.Pattern (i.e. before creating a Matcher)

Example from the Java docs:

Capturing groups are numbered by counting their opening parentheses from left to right. In the expression ((A)(B(C))), for example, there are four such groups:

1         ((A)(B(C)))
2         (A)
3         (B(C))
4         (C)

In principle it should be possible to identify these from the (compiled) Pattern.

UPDATE: From @Leniel and eslewhere it seems that this f开发者_运维百科acility ("named groups") will be present in Java 7 in mid 2011. If I can't wait for that I can use jregex although I'm not quite sure what the API is.


You can find out the number of groups by creating a dummy Matcher, like so:

Pattern p = Pattern.compile("((A)(B(C)))");
System.out.println(p.matcher("").groupCount());

If you want the actual subexpressions (((A)(B(C))), (A), etc.), then no, that information is not available.


Yes. Check this:

Regex Named Groups in Java

0

精彩评论

暂无评论...
验证码 换一张
取 消