The example produces the following output:
one caterpillar, two caterpillars, or three caterpillars on a fence
Two other text replacement methods make it possible to replace either the first match or all matches with replacement text:
- public String replaceFirst(String replacement): resets the matcher, creates a new String object, copies all the matcher's text characters (up to the first match) to String, appends the replacement characters to String, copies remaining characters to String, and returns that object's reference. (The replacement string may contain references to text sequences captured during the previous match, via dollar-sign characters and capturing group numbers.)
- public String replaceAll(String replacement): operates similarly to the previous method. However, replaceAll(String replacement) replaces all matches with replacement's characters.
The \s+ regex detects one or more occurrences of whitespace characters in text. The following example uses that regex and calls the replaceAll(String replacement) method to remove duplicate whitespace from text:
Pattern p = Pattern.compile ("\\s+");
Matcher m = p.matcher ("Remove the \t\t duplicate whitespace. ");
System.out.println (m.replaceAll (" "));
The example produces the following output:
Remove the duplicate whitespace.
Listing 1 includes System.out.println ("Found " + m.group ());. Notice the call to group(). That method is one of three capturing group-oriented Matcher methods:
- public int groupCount(): returns the number of capturing groups in a matcher's pattern. That count does not include the special capturing group number 0, which captures the previous match (whether or not a pattern includes capturing groups).
- public String group(): returns the previous match's characters as recorded by capturing group number 0. That method may return an empty string to indicate a successful match against the empty string. An IllegalStateException object is thrown if either the matcher has not yet attempted a match or the previous match operation failed.
- public String group(int group): resembles the previous method, except it returns the previous match's characters as recorded by the capturing group number that group specifies. If no capturing group with the specified group number exists in the pattern, the method throws an IndexOutOfBoundsException object.
No comments:
Post a Comment