Java 中的 Matcher reset() 方法示例

javaobject oriented programmingprogramming更新于 2024/5/8 13:32:17

java.util.regex.Matcher 类表示执行各种匹配操作的引擎。此类没有构造函数,您可以使用 java.util.regex.Pattern 类的 matches() 方法创建/获取此类的对象。

此 (Matcher) 类的 reset() 方法删除所有状态信息并将字符序列重置为默认值,并将位置附加为零。

示例 1

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>";
      //创建一个 Pattern 对象
      Pattern pattern = Pattern.compile(regex);
      //匹配字符串中的编译模式
      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());
   }
}

输出

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

此方法的另一种变体接受字符串数据并使用它重置 Matcher。

示例 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+)";
      //创建一个 Pattern 对象
      Pattern pattern = Pattern.compile(regex);
      //匹配字符串中的编译模式
      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());
      }
   }
}

输出

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

相关文章


有用资源