Java Replacing the Matched Text

Java Replacing the Matched Text


As we saw in the previous recipe, regex patterns involving multipliers can match a lot
of characters with very few metacharacters. We need a way to replace the text that
the regex matched without changing other text before or after it. We could do this
manually using the String method substring( ) . However, because it’s such a com-
mon requirement, the JDK 1.4 Regular Expression API provides some substitution
methods. In all these methods, you pass in the replacement text or “right-hand side”
of the substitution (this term is historical: in a command-line text editor’s substitute
command, the left-hand side is the pattern and the right-hand side is the replace-
ment text). The replacement methods are:

replaceAll(newString)
Replaces all occurrences that matched with the new string.

appendReplacement(StringBuffer, newString)
Copies up to before the first match, plus the given newString .

appendTail(StringBuffer)
Appends text after the last match (normally used after appendReplacement ).

Example 4-2 shows use of these three methods.

ReplaceDemo.java
// class ReplaceDemo
// Quick demo of substitution: correct "demon" and other
// spelling variants to the correct, non-satanic "daemon".
// Make a regex pattern to match almost any form (deamon, demon, etc.).
String patt = "d[ae]{1,2}mon"; // i.e., 1 or 2 'a' or 'e' any combo
// A test string.
String input = "Unix hath demons and deamons in it!";
System.out.println("Input: " + input);
// Run it from a regex instance and see that it works
Pattern r = Pattern.compile(patt);
Matcher m = r.matcher(input);
System.out.println("ReplaceAll: " + m.replaceAll("daemon"));
// Show the appendReplacement method
m.reset( );
StringBuffer sb = new StringBuffer( );
System.out.print("Append methods: ");
while (m.find( )) {
m.appendReplacement(sb, "daemon");
}
m.appendTail(sb);
System.out.println(sb.toString( ));
// Copy to before first match,
// plus the word "daemon"
// copy remainder

Sure enough, when you run it, it does what we expect:

Input: Unix hath demons and deamons in it!
ReplaceAll: Unix hath daemons and daemons in it!
Append methods: Unix hath daemons and daemons in it!

0 comments:

Post a Comment