English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Matcher reset() method in Java with example

Thisjava.util.regex.Matcher'sThis class represents an engine for performing various matching operations. This class has no constructor and can be used withmatches()The method of the class java.util.regex.Pattern creates/Get the object of this class.

Inreset()The methods of this (Matcher) class remove all state information and reset the character sequence to the default value, with the append position at 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 the content of bold tags
      String regex = "<b>(\\S+)</b>";
      //Create a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Match 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 Result

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 string data and uses it to reset the Matcher.

Example2 

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 the content of bold tags
      String regex = "(\\S+)";
      //Create a pattern object
      Pattern pattern = Pattern.compile(regex);
      //Match 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 Result

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>]