importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassResetting{publicstaticvoidmain(String[] args)throwsException{Matcher m =Pattern.compile("[frb][aiu][gx]").matcher("fix the rug with bags");while(m.find())System.out.println(m.group());
m.reset("fix the rig with rags");while(m.find())System.out.println(m.group());}}
结果
上面的代码示例将产生以下结果。
fix
rug
bag
fix
rig
rag
以下是重置正则表达式模式的另一个示例
importjava.util.regex.Matcher;importjava.util.regex.Pattern;publicclassMatc{publicstaticvoidmain(String args[]){Pattern p =Pattern.compile("\\d");Matcher mat1 = p.matcher("9652018244");while(mat1.find()){System.out.println("\t\t"+ mat1.group());}
mat1.reset();System.out.println("After done resetting the Matcher, it should be like this");while(mat1.find()){System.out.println("\t\t"+ mat1.group());}}}
结果
上面的代码示例将产生以下结果。
9652018244After done resetting the Matcher, it should be like this9652018244