public String evaluate(String s, String regex, Integer extractIndex) {
    if (s == null || regex == null) {
      return null;
    }
    if (!regex.equals(lastRegex) || p == null) {
      lastRegex = regex;
      p = Pattern.compile(regex);
    }
    Matcher m = p.matcher(s);
    if (m.find()) {
      MatchResult mr = m.toMatchResult();
      return mr.group(extractIndex);
    }
    return "";
  }
UDF to extract a specific group identified by a java regex. Note that if a
regexp has a backslash ('\'), then need to specify '\\' For example,
regexp_extract('100-200', '(\\d+)-(\\d+)', 1) will return '100'
ql/src/java/org/apache/hadoop/hive/ql/udf/UDFRegExpExtract.java