private String replaceMatchGroupValues()

in RegularExpressionAnnotator/src/main/java/org/apache/uima/annotator/regex/impl/RegExAnnotator.java [901:977]


  private String replaceMatchGroupValues(String featureValue, Matcher matcher, Rule rule)
      throws RegexAnnotatorProcessException {
    StringBuffer replaced = new StringBuffer();
    int pos = 0;
    int end = featureValue.length();
    char c;
    // Iterate over the input text to find the match groups that must be
    // replaced.
    // In the input text, all $ and \ characters must be escaped by \.
    while (pos < end) {
      c = featureValue.charAt(pos);
      // Everything followed by a \ was escaped and the \ (escape character)
      // can be removed now
      if (c == '\\') {
        // skip escape character
        ++pos;

        // add escaped character to the output
        if (pos < end) {
          replaced.append(featureValue.charAt(pos));
          // go to the next character
          ++pos;
        }
      } else if (c == '$') {
        // this must be a match group $n since all other $ characters must
        // be escaped with a \ which is handled above.
        // skip $ character we are only interested in the match group number
        // or name
        // match group name syntax is ${match group name}
        ++pos;
        if (pos < end) {
          // get next char to check if we have a match group number or a
          // match group name
          c = featureValue.charAt(pos);

          int groupNumber = -1;
          if (c == '{') {
            // we have a match group name
            // skip grace '{'
            ++pos;
            // get match group name
            int matchNameEnd = featureValue.indexOf("}", pos);
            if (matchNameEnd > -1) {
              String matchGroupName = featureValue.substring(pos, matchNameEnd);
              // get match group number for the given match group name
              groupNumber = rule.getMatchGroupNumber(matchGroupName);
              if (groupNumber == -1) {
                throw new RegexAnnotatorProcessException(
                    "regex_annotator_error_match_group_name_not_found", new Object[] {
                        matchGroupName, rule.getId() });
              }
              // set pos to the end of the match group name syntax
              pos = matchNameEnd + 1;
            }
          } else {
            // we have a match group number
            // convert match group number to integer value
            groupNumber = c - '0';
            // skip match group number
            ++pos;
          }

          // get match group content
          String groupMatch = matcher.group(groupNumber);
          // add match group content to the output
          if (groupMatch != null) {
            replaced.append(groupMatch);
          }
        }
      } else {
        // default output character that is added to the output
        replaced.append(c);
        ++pos;
      }
    }
    return replaced.toString();
  }