private static int groupCount(String regex){
  regex=" " + regex;
  int idxOpen=regex.indexOf('[');
  if (idxOpen != -1) {
    int idxClose=regex.indexOf(']',idxOpen);
    if (idxClose != -1) {
      regex=regex.substring(0,idxOpen) + regex.substring(idxClose + 1);
    }
  }
  Matcher matcher=GROUP.matcher(regex);
  int groupCount=0;
  while (matcher.find())   groupCount++;
  return groupCount;
}

private static int groupCount ( String regex ) { regex = " " + regex ; int idxOpen = regex . indexOf ( '[' ) ; if ( idxOpen != - 1 ) { int idxClose = regex . indexOf ( ']' , idxOpen ) ; if ( idxClose != - 1 ) { regex = regex . substring ( 0 , idxOpen ) + regex . substring ( idxClose + 1 ) ; } } Matcher matcher = GROUP . matcher ( regex ) ; int groupCount = 0 ; while ( matcher . find ( ) ) groupCount ++ ; return groupCount ; }
find the number of groups in the regular expression don ' t count escaped ' ( '
