XML technologies / Regular expressions / Java / The Matcher object / In-place Search and replace

(Excerpt from "Regular expressions" by Jeffrey E. F. Friedl)

So far, we have applied java.util.regex only to String objects. Since a Matcher can be created for any object implementing interface CharSequence , text can also be modified directly.

The usual classes are StringBuffer and StringBuilder, the former being less efficient but thread-safe. Both can replace String objects while also supporting direct modification. This book’s examples use StringBuilder , but StringBuffer works equally well in a multithreaded environment.

The following example replaces all-uppercase words in a StringBuilder object with their lowercase equivalents. Note that this example’s regex is ˹\b[\p{Lu}\p{Lt}]+\b˼. As explained in Features and dialects, \p{Lu} matches all Unicode uppercase letters, and \p{Lt} matches titlecase letters. The ASCII version would be ˹\b[A-Z]+\b˼.

StringBuilder text = new StringBuilder("Hör ENDLICH auf RUMZUBRÜLLEN!");
Matcher m = Pattern.compile("\\b[\\p{Lu}\\p{Lt}]+\\b").matcher(text);
while (m.find())
   text.replace(m.start(), m.end(), m.group().toLowerCase());
System.out.println(text);

The fragment prints:

Hör endlich auf rumzubrüllen!

The two matches produce two calls to text.replace. The first two parameters specify the replacement range, simply the match’s start and end; the third specifies the inserted text, the lowercase match.

In-place replacement is simple when, as here, replacement text has the same length as the match. Repeatedly replacing only the first match is equally straightforward.

 

Replacement text of a different length

Processing becomes harder when replacement and match lengths differ. Changing the input string during processing may shift the current position.

We can solve this by explicitly tracking the current position and passing it as the starting value to method find . This example encloses replacement text in <b>...</b>.

StringBuilder text = new StringBuilder("Hör ENDLICH auf RUMZUBRÜLLEN!");
Matcher m = Pattern.compile("\\b[\\p{Lu}\\p{Lt}]+\\b").matcher(text); 
int matchZeiger = 0;      // Die erste Suche beginnt am Anfang des Strings.
while (m.find(matchZeiger)) {
   matchZeiger = m.end(); // Die nächste beginnt, wo die vorherige aufgehört hat.
   text.replace(m.start(), m.end(), "<b>"+ m.group().toLowerCase() +"</b>");
   matchZeiger += 7;      // Zusätzliche 7 Zeichen (fl<b>fl und fl</b>fl).
}
System.out.println(text);

This gives:

Hör <b>endlich</b> auf <b>rumzubrüllen</b>!

  

 

 

 

German edition copyright © 2008 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for private use.
Otherwise, this chapter from "Reguläre Ausdrücke" is subject to the same terms as the bound edition: the work and all its parts are protected by copyright. All rights reserved, including reproduction, translation, microfilming, storage and processing in electronic systems.

O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln

back to top