### Question:
PatternTemplate { public String applyTo(final MatchResult result) { int len = value.length(); int groups = result.groupCount(); StringBuilder sb = new StringBuilder(); boolean escaped = false; for (int n = 0; n < len; n++) { char c = value.charAt(n); if (escaped) { sb.append(c); escaped = false; } else if (c == '\\') { escaped = true; } else if (c == '$') { int group = -1; while (n + 1 < len) { int digit = value.charAt(n + 1) - '0'; if (digit < 0 || digit > 9) { break; } group = (group == -1 ? 0 : group) * 10 + digit; n++; } if (group >= 0 && group <= groups) { sb.append(result.group(group)); } } else { sb.append(c); } } return sb.toString(); } PatternTemplate(final String value); String applyTo(final MatchResult result); @Override String toString();  }### Answer:
@Test public void testTemplateSubstitutionWithoutGroupCapture() throws Exception { PatternTemplate template = new PatternTemplate("matched"); Pattern pattern = Pattern.compile(".*"); Matcher matcher = pattern.matcher("Hello"); assertThat(matcher.matches()).isTrue(); assertThat(template.applyTo(matcher.toMatchResult())) .isEqualTo("matched"); }
@Test public void testTemplateSubstitutionWithGroup0Capture() throws Exception { PatternTemplate template = new PatternTemplate("$0"); Pattern pattern = Pattern.compile(".*"); Matcher matcher = pattern.matcher("Hello"); assertThat(matcher.matches()).isTrue(); assertThat(template.applyTo(matcher.toMatchResult())) .isEqualTo("Hello"); }
@Test public void testTemplateSubstitutionWithGroup1Capture() throws Exception { PatternTemplate template = new PatternTemplate("$1"); Pattern pattern = Pattern.compile("(Hel+)o"); Matcher matcher = pattern.matcher("Hello"); assertThat(matcher.matches()).isTrue(); assertThat(template.applyTo(matcher.toMatchResult())) .isEqualTo("Hell"); }
@Test public void testTemplateSubstitutionWithTwoDigitGroupCapture() throws Exception { PatternTemplate template = new PatternTemplate("$10"); Pattern pattern = Pattern.compile("(((He(l+))(o)) (((Op)(en))(IG)))"); Matcher matcher = pattern.matcher("Hello OpenIG"); assertThat(matcher.matches()).isTrue(); assertThat(template.applyTo(matcher.toMatchResult())) .isEqualTo("IG"); }
@Test public void testTemplateSubstitutionWithMixedStringAndGroupCapture() throws Exception { PatternTemplate template = new PatternTemplate("$1o OpenIG"); Pattern pattern = Pattern.compile("(Hel+)o"); Matcher matcher = pattern.matcher("Hello"); assertThat(matcher.matches()).isTrue(); assertThat(template.applyTo(matcher.toMatchResult())) .isEqualTo("Hello OpenIG"); }
@Test public void testTemplateSubstitutionWithEscapedDollar() throws Exception { PatternTemplate template = new PatternTemplate("\\$1"); Pattern pattern = Pattern.compile(".*"); Matcher matcher = pattern.matcher("Hello"); assertThat(matcher.matches()).isTrue(); assertThat(template.applyTo(matcher.toMatchResult())) .isEqualTo("$1"); }
@Test public void testTemplateSubstitutionWithEscapedSlash() throws Exception { PatternTemplate template = new PatternTemplate("a\\\\b"); Pattern pattern = Pattern.compile(".*"); Matcher matcher = pattern.matcher("Hello"); assertThat(matcher.matches()).isTrue(); assertThat(template.applyTo(matcher.toMatchResult())) .isEqualTo("a\\b"); }