Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying capture groups in a Regex Pattern

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 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.

like image 704
peter.murray.rust Avatar asked Jan 21 '23 19:01

peter.murray.rust


2 Answers

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.

like image 144
Alan Moore Avatar answered Jan 31 '23 04:01

Alan Moore


Yes. Check this:

Regex Named Groups in Java

like image 34
Leniel Maccaferri Avatar answered Jan 31 '23 05:01

Leniel Maccaferri