
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Matcher reset() method in Java with Examples
The java.util.regex.Matcher class represents an engine that performs various match operations. There is no constructor for this class, you can create/obtain an object of this class using the matches() method of the class java.util.regex.Pattern.
The reset() method of this (Matcher) class removes all the state information and resets the character sequence to default, and append position to zero.
Example1
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Reset { public static void main(String[] args) { String str = "<p>This <b>is</b> an <b>example</b>HTML <b>script</b> where <b>every</b> alternative <b>word</b> is <b>bold</b></p>."; //Regular expression to match contents of the bold tags String regex = "<b>(\S+)</b>"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println("State of the matcher: "+matcher.toMatchResult()); String result = matcher.group(1); } matcher = matcher.reset(); System.out.println("State of the matcher after resetting it: "+matcher.toMatchResult()); } }
Output
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>is</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>example</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>script</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>every</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>>word</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>bold</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=] State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=]
Another variant of this method accepts a string data and resets the Matcher with it.
Example 2
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Reset { public static void main(String[] args) { String str = "<p>This <b>is</b> an <b>example</b> HTML <b>script</b> where <b>every</b> alternative <b>word</b> is <b>bold</b></p>."; //Regular expression to match contents of the bold tags String regex = "(\S+)"; //Creating a pattern object Pattern pattern = Pattern.compile(regex); //Matching the compiled pattern in the String Matcher matcher = pattern.matcher(str); while (matcher.find()) { System.out.println("State of the matcher: "+matcher.toMatchResult()); String result = matcher.group(1); } matcher = matcher.reset("<b>this</b> is <b>new</b> string <b>after</b> reset"); while (matcher.find()) { System.out.println("State of the matcher after resetting it: "+matcher.toMatchResult()); } } }
Output
State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>is</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>example</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>script</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>every</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>word</b>] State of the matcher: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,116 lastmatch=<b>bold</b>] State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>this</b>] State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>new</b>] State of the matcher after resetting it: java.util.regex.Matcher[pattern=<b>(\S+)</b> region=0,51 lastmatch=<b>after</b>]
- Related Articles
- Matcher start() method in Java with Examples
- Matcher matches() method in Java with Examples
- Pattern matcher() method in Java with examples
- Matcher pattern() method in Java with Examples
- Matcher replaceAll() method in Java with Examples
- Matcher replaceFirst() method in Java with Examples
- Matcher requireEnd() method in Java with Examples
- Matcher toString() method in Java with Examples
- Matcher appendTail() method in Java with Examples
- Matcher group() method in Java with Examples
- Matcher groupCount() method in Java with Examples
- Matcher appendReplacement() method in Java with Examples
- Matcher regionEnd() method in Java with Examples
- Matcher regionStart() method in Java with Examples
- Matcher toMatchResult() method in Java with Examples

Advertisements