import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatcherGroupCountExample { public static void main(String args[]) { Pattern p = Pattern.compile("B(ond)"); String candidateString = "My name is Bond. James Bond."; Matcher matcher = p.matcher(candidateString); int numberOfGroups = matcher.groupCount(); System.out.println("numberOfGroups =" + numberOfGroups); } }