Matcher groupCount() method in Java with Examples
Last Updated :
26 Nov, 2018
The groupCount() method of Matcher Class is used to get the number of capturing groups in this matcher’s pattern.
Syntax:
public int groupCount()
Parameters: This method do not takes any parameter.
Return Value: This method returns the number of capturing groups in this matcher’s pattern.
Below examples illustrate the Matcher.groupCount() method:
Example 1:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "Geeks" ;
Pattern pattern
= Pattern.compile(regex);
String stringToBeMatched
= "GeeksForGeeks" ;
Matcher matcher
= pattern
.matcher(stringToBeMatched);
System.out.println(matcher.groupCount());
}
}
|
Example 2:
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "GFG" ;
Pattern pattern
= Pattern.compile(regex);
String stringToBeMatched
= "GFGFGFGFGFGFGFGFGFG" ;
Matcher matcher
= pattern
.matcher(stringToBeMatched);
System.out.println(matcher.groupCount());
}
}
|
Reference: Oracle Doc
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...