### Question:
RegexTemplate { public String apply( String input ) { String output = outputTemplate; Matcher inputMatcher = inputPattern.matcher( input ); if( inputMatcher.find() ) { output = expandTemplate( inputMatcher, output ); } return output; } RegexTemplate( String regex, String template );  RegexTemplate( String regex, String template, Map<String,String> map, boolean useOriginalOnLookupFailure ); String apply( String input );  }### Answer:
@Test public void testExtractUsernameFromEmailAddress() { RegexTemplate template; String actual; template = new RegexTemplate( "(.*)@.*", "prefix_{1}_suffix" ); actual = template.apply( "member@apache.org" ); assertThat( actual, is( "prefix_member_suffix" ) ); template = new RegexTemplate( "(.*)@.*", "prefix_{0}_suffix" ); actual = template.apply( "member@apache.org" ); assertThat( actual, is( "prefix_member@apache.org_suffix" ) ); template = new RegexTemplate( "(.*)@.*", "prefix_{1}_{a}_suffix" ); actual = template.apply( "member@apache.org" ); assertThat( actual, is( "prefix_member_{a}_suffix" ) ); }
@Test public void testExtractUsernameFromEmailAddressAndMapDomain() { RegexTemplate template; Map<String,String> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); map.put( "us", "USA" ); map.put( "ca", "CANADA" ); String actual; template = new RegexTemplate( "(.*)@(.*?)\\..*", "prefix_{1}:{[2]}_suffix", map, false ); actual = template.apply( "member@us.apache.org" ); assertThat( actual, is( "prefix_member:USA_suffix" ) ); actual = template.apply( "member@ca.apache.org" ); assertThat( actual, is( "prefix_member:CANADA_suffix" ) ); actual = template.apply( "member@nj.apache.org" ); assertThat( actual, is( "prefix_member:_suffix" ) ); }
@Test public void testLookupFailure() { RegexTemplate template; Map<String,String> map = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); map.put( "us", "USA" ); map.put( "ca", "CANADA" ); String actual; template = new RegexTemplate( "(.*)@(.*?)\\..*", "prefix_{1}:{[2]}_suffix", map, true ); actual = template.apply( "member@us.apache.org" ); assertThat( actual, is( "prefix_member:USA_suffix" ) ); actual = template.apply( "member@ca.apache.org" ); assertThat( actual, is( "prefix_member:CANADA_suffix" ) ); actual = template.apply( "member@nj.apache.org" ); assertThat( actual, is( "prefix_member:nj_suffix" ) ); }