Java Matcher groupCount() Method
Example
Count the groups in a date pattern:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("(\\d{4})-(\\d{2})-(\\d{2})");
Matcher matcher = pattern.matcher("2014-03-18");
System.out.println(matcher.groupCount());
}
}
Definition and Usage
The groupCount() method returns the number of groups in the pattern. A group is a part of the pattern inside parentheses ( ).
Group 0, the whole match, is not counted. You can call groupCount() before any search is done.
Tip: Use the group() method to get the text of each group.
Syntax
public int groupCount()
Parameter Values
None.
Technical Details
| Returns: | The number of groups in the pattern, as an int. |
|---|
Related Pages
❮ RegEx Methods