You use appendReplacement and appendTail directly when you want to process the input in some other--or additional--manner than replaceAll does. For instance, if you want to use the matched text as a key to look up the replacement value in a table, you have to write your own replaceAll equivalent. Here's an example I happened to have handy:
- import java.util.*;
- import java.util.regex.*;
- class Test
- {
- static Map props = new HashMap();
- static
- {
- props.put("key1", "fox");
- props.put("key2", "dog");
- }
- public static void main(String[] args)
- {
- String input = "The quick brown ${key1} jumps over the lazy ${key2}.";
- Pattern p = Pattern.compile("\\$\\{([^}]+)\\}");
- Matcher m = p.matcher(input);
- StringBuffer sb = new StringBuffer();
- while (m.find())
- {
- m.appendReplacement(sb, "");
- sb.append(props.get(m.group(1)));
- }
- m.appendTail(sb);
- System.out.println(sb.toString());
- }
- }
No comments:
Post a Comment