Java Matcher reset() Method
Example
Search a text two times, and then reset the matcher to search again:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Main {
public static void main(String[] args) {
Pattern pattern = Pattern.compile("w3schools", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher("Visit W3Schools!");
System.out.println(matcher.find());
System.out.println(matcher.find());
matcher.reset();
System.out.println(matcher.find());
}
}
Definition and Usage
The reset() method resets a matcher, so the next search starts from the beginning of the text again.
In the example, the first find() finds W3Schools. The second find() finds nothing, because there are no more matches. After reset(), find() finds W3Schools again.
Tip: Use reset(input) to search a new text with the same matcher.
Syntax
One of the following:
public Matcher reset()
public Matcher reset(CharSequence input)
Parameter Values
| Parameter | Description |
|---|---|
| input | Optional. A new text to search. |
Technical Details
| Returns: | The same Matcher object. |
|---|
Related Pages
❮ RegEx Methods