code: public static String extractRegexGroup(String fromContent, Pattern regex, int groupNumber) throws Exception {
          if (fromContent == null) {
              throw new Exception("Cannot extract regex group because the provided content string is null.");
          }
          if (regex == null) {
              throw new Exception("Cannot extract regex group because the provided regular expression is null.");
          }
          if (groupNumber < 0) {
              throw new Exception("Group number to extract must be non-negative. Provided group number was " + groupNumber);
          }
          Matcher matcher = regex.matcher(fromContent);
          if (!matcher.find()) {
              throw new Exception("Did not find any matches for regex [" + regex + "] in the provided content: [" + fromContent + "].");
          }
          if (matcher.groupCount() < groupNumber) {
              throw new Exception("Found " + matcher.groupCount() + " matching groups in the content, but expected at least " + groupNumber + ". Regex was [" + regex + "] and content was [" + fromContent + "].");
          }
          return matcher.group(groupNumber);
      }
docs: Extracts the specified matching group in the provided content. An exception is thrown if the group number is invalid
  (negative or greater than the number of groups found in the content), or if a matching group cannot be found in the
  content.