Java中正则Matcher类的matches()、lookAt()和find()的差别
參考博文地址:http://www.oseye.net/user/kevin/blog/170
1、matcher():僅僅有在整個字符串全然匹配才返回true,否則返回false。
可是假設部分匹配成功。匹配的位置將移動到下次匹配的位置
2、lookingAt():總是從第一個字符開始匹配。不管匹配成功與否,都不會再繼續向下匹配
3、find():部分匹配,假設匹配成功。返回true,匹配的位置移動到下次匹配的位置。
但假設前部分匹配成功,將移動下次匹配的位置*/ System.out.println(matcher.matches()); /*false*/ /*測試匹配的位置*/ matcher.find(); System.out.println(matcher.start()); /*4*/ /*重置匹配的位置*/ matcher.reset(); /*find:部分匹配,從當前位置開始匹配,找到一個匹配的子串,將移動下次匹配的位置*/ System.out.println(matcher.find());/*true*/ System.out.println(matcher.group() + "---" + matcher.start());/*123---0*/ System.out.println(matcher.find());/*true*/ System.out.println(matcher.group() + "---" + matcher.start());/*34345---4*/ /*lookingAt:部分匹配。總是從第一個字符進行匹配,匹配成功了不再繼續匹配。匹配失敗了,也不繼續匹配。
*/ System.out.println(matcher.lookingAt());/*true*/ System.out.println(matcher.group() + "---" + matcher.start());/*123---0*/ System.out.println(matcher.lookingAt());/*true*/ System.out.println(matcher.group() + "---" + matcher.start());/*123-0*/ } }
轉載于:https://www.cnblogs.com/cxchanpin/p/6865697.html
總結
以上是生活随笔為你收集整理的Java中正则Matcher类的matches()、lookAt()和find()的差别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Caffe上用SSD训练和测试自己的数据
- 下一篇: 关于虚函数的应用(10个例子)