public String processLine(String line) {
    for (Entry<Pattern, String> r : replacements.entrySet()) {
      Matcher m = r.getKey().matcher(line);
      if(m.find()) {
        line = m.replaceAll(r.getValue());
      }
    }
    return line;
  }
QTest replacement directive handler

Examples:

--! qt:replace:/there/joe/
select 'hello there!
===q.out
hello joe!

standard java regex; placeholders also work:
--! qt:replace:/Hello (.*)!/$1 was here!/

first char of regex pattern is used as separator; you may choose anything else than '/'
--! qt:replace:#this#that#
itests/util/src/main/java/org/apache/hadoop/hive/ql/qoption/QTestReplaceHandler.java