Java Reference
In-Depth Information
public int start(int group)
Returns the start index of the given group from the previous
match.
public int end(int group)
Returns the index of the last character matched of the given
group, plus one.
Together these methods form the MatchResult interface, which allows a
match result to be queried but not modified. You can convert the cur-
rent matcher state to a MatchResult instance by invoking its toMatchResult
method. Any subsequent changes to the matcher state do not affect the
existing MatchResult objects.
13.3.3. Replacing
You will often want to pair finding matches with replacing the matched
characters with new ones. For example, if you want to replace all in-
stances of sun with moon , your code might look like this: [2]
[2] The StringBuffer class (see page 335 ) is an appendable character sequence (you can modify its
contents). The Matcher class should have been updated in the 5.0 release to work with any append-
able character sequence, such as StringBuilder , but this was overlooked.
Pattern pat = Pattern.compile("sun");
Matcher matcher = pat.matcher(input);
StringBuffer result = new StringBuffer();
boolean found;
while ((found = matcher.find()))
matcher.appendReplacement(result, "moon");
matcher.appendTail(result);
 
 
Search WWH ::




Custom Search