识别正则表达式模式中的捕获组

时间:2022-11-25 21:45:29

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)

Java中是否有一种方法(可能还有一个额外的开源库)来识别java.util.regex.Pattern中的捕获组(即在创建匹配器之前)

Example from the Java docs:

Java文档中的示例:

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:

捕获组通过从左到右计算它们的左括号来编号。在表达式((A)(B(C)))中,例如,有四个这样的组:

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 facility ("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.

更新:从@Leniel和eslewhere看来,这个工具(“命名组”)将在2011年中期出现在Java 7中。如果我不能等待,我可以使用jregex虽然我不太确定API是什么是。

2 个解决方案

#1


7  

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.

如果你想要实际的子表达式(((A)(B(C))),(A)等),那么不,该信息不可用。

#2


2  

Yes. Check this:

是。检查一下:

Regex Named Groups in Java

Java中的正则表达式命名组

#1


7  

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.

如果你想要实际的子表达式(((A)(B(C))),(A)等),那么不,该信息不可用。

#2


2  

Yes. Check this:

是。检查一下:

Regex Named Groups in Java

Java中的正则表达式命名组