Recipe 4.4 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 common 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 replacement text). The replacement methods are:


Replaces all occurrences that matched with the new string.

appendReplacement(StringBuffer, newString)

Copies up to before the first match, plus the given newString.


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

Example 4-2 shows use of these three methods.

Example 4-2.
// 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");  // Copy to before first match,                                         // plus the word "daemon" } m.appendTail(sb);                       // copy remainder System.out.println(sb.toString( ));

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!

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: