MatchResult groupCount() method in Java with Examples
The groupCount() method of MatchResult Interface is used to get the number of capturing groups in this matcher’s pattern. This method returns an integer value which is the number of groups found while matching this matcher.
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 MatchResult.groupCount() method:
Example 1:
Java
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "(Geeks)" ;
Pattern pattern
= Pattern.compile(regex);
String stringToBeMatched
= " GeeksForGeeks Geeks for For Geeks Geek" ;
MatchResult matcher
= pattern
.matcher(stringToBeMatched);
System.out.println(matcher.groupCount());
}
}
|
Example 2:
Java
import java.util.regex.*;
public class GFG {
public static void main(String[] args)
{
String regex = "GFG" ;
Pattern pattern
= Pattern.compile(regex);
String stringToBeMatched
= "FGF GF FG FGF" ;
MatchResult matcher
= pattern
.matcher(stringToBeMatched);
System.out.println(matcher.groupCount());
}
}
|
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/regex/Matcher.html#groupCount–
Last Updated :
04 Dec, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...