该java.util.regex.Matcher中的类代表一个引擎,进行各种匹配操作。该类没有构造函数,可以使用matches()
类java.util.regex.Pattern的方法创建/获取该类的对象。
在reset()
此(匹配器)类的方法除去所有的状态信息和重置该字符序列到默认值,追加位置为零。
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>."; //正则表达式匹配粗体标签的内容 String regex = "<b>(\\S+)</b>"; //创建一个模式对象 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。
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>."; //正则表达式匹配粗体标签的内容 String regex = "(\\S+)"; //创建一个模式对象 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>]