Matcher reset() method in Java with Examples
java.util.regex.Matcher类表示执行各种匹配操作的引擎。 该类没有构造函数,可以使用类java.util.regex.Pattern的matchs()方法创建/获取该类的对象。
这个(Matcher)类的reset()方法删除所有状态信息,并将字符序列重置为默认值,并将位置附加到零。
例1
<!-
现场演示
->
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.regex.Matcher; import java.util.regex.Pattern; public class Reset { public static void main(String[] args) { String str ="<p> This is an exampleHTML script where every alternative word is bold </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(); System.out.println("State of the matcher after resetting it:"+matcher.toMatchResult()); } } |
输出量
1 2 3 4 5 6 7 8 | State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=is] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=example] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=script] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=every] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=>word] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=bold] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=] State of the matcher after resetting it: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=] |
此方法的另一个变体接受字符串数据并使用它重置Matcher。
例子2
<!-
现场演示
->
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.regex.Matcher; import java.util.regex.Pattern; public class Reset { public static void main(String[] args) { String str ="<p> This is an example HTML script where every alternative word is bold </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("this is new string after reset"); while (matcher.find()) { System.out.println("State of the matcher after resetting it:"+matcher.toMatchResult()); } } } |
输出量
1 2 3 4 5 6 7 8 9 | State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=is] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=example] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=script] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=every] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=word] State of the matcher: java.util.regex.Matcher[pattern=(\S+) region=0,116 lastmatch=bold] State of the matcher after resetting it: java.util.regex.Matcher[pattern=(\S+) region=0,51 lastmatch=this] State of the matcher after resetting it: java.util.regex.Matcher[pattern=(\S+) region=0,51 lastmatch=new] State of the matcher after resetting it: java.util.regex.Matcher[pattern=(\S+) region=0,51 lastmatch=after] |