private String getResult(String origin, Matcher matcher) {
    String result;
    if (reverse) {
      IntArrayList endIndexList = new IntArrayList();
      while (matcher.find()) {
        endIndexList.add(matcher.end());
      }
      String suffix;
      if (endIndexList.size() < offset + 1) {
        suffix = origin;
      } else {
        suffix = origin.substring(endIndexList.get(endIndexList.size() - offset - 1));
      }
      String prefix = "";
      if (limit != -1 && endIndexList.size() >= limit + offset + 1) {
        prefix = origin.substring(0, endIndexList.get(endIndexList.size() - limit - offset - 1));
      }
      result =
          prefix
              .concat(
                  origin
                      .substring(prefix.length(), origin.length() - suffix.length())
                      .replaceAll(regex, replace))
              .concat(suffix);
    } else {
      IntArrayList fromIndexList = new IntArrayList();
      while (matcher.find() && fromIndexList.size() < limit + offset + 2) {
        fromIndexList.add(matcher.start());
      }
      String prefix = origin;
      if (fromIndexList.size() >= offset + 1) {
        prefix = origin.substring(0, fromIndexList.get(offset));
      }
      String suffix = "";
      if (limit != -1 && fromIndexList.size() >= limit + offset + 1) {
        suffix = origin.substring(fromIndexList.get(limit + offset));
      }
      result =
          prefix
              .concat(
                  origin
                      .substring(prefix.length(), origin.length() - suffix.length())
                      .replaceAll(regex, replace))
              .concat(suffix);
    }

    return result;
  }
This function replaces substring according to regex parameter from an input series.
library-udf/src/main/java/org/apache/iotdb/library/string/UDTFRegexReplace.java