Java正则表达式Matcher的find()和matches()方法不同
2018-01-18 11:00
375 查看
1、直接上结论:
find()方法在部分匹配时和完全匹配时返回true,匹配不上返回false;
matches()方法只有在完全匹配时返回true,匹配不上和部分匹配都返回false。
2、测试该结论的例子如下:
String
regex = "own\\.png";
String
input= "down.png";
Pattern
p = Pattern.compile(regex);
Matcher
m = p.matcher(input);
System.out.println(m.matches());//false
m.reset();
System.out.println(m.find());//true
String regex = "down\\.png";
String input = "down.png";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.out.println(m.matches());//true
m.reset();
System.out.println(m.find());//true
3、验证该结论需要查看源码。
find()方法注释最后部分对return true进行解释: if, and only if, a subsequence of the input sequence matches this matcher's pattern,根据这句解释整个input也算是a subsequence;
matches()方法注释最后部分对return true进行解释: if, and only if, the entire region sequence matches this matcher's pattern。
find()方法在部分匹配时和完全匹配时返回true,匹配不上返回false;
matches()方法只有在完全匹配时返回true,匹配不上和部分匹配都返回false。
2、测试该结论的例子如下:
String
regex = "own\\.png";
String
input= "down.png";
Pattern
p = Pattern.compile(regex);
Matcher
m = p.matcher(input);
System.out.println(m.matches());//false
m.reset();
System.out.println(m.find());//true
String regex = "down\\.png";
String input = "down.png";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
System.out.println(m.matches());//true
m.reset();
System.out.println(m.find());//true
3、验证该结论需要查看源码。
find()方法注释最后部分对return true进行解释: if, and only if, a subsequence of the input sequence matches this matcher's pattern,根据这句解释整个input也算是a subsequence;
matches()方法注释最后部分对return true进行解释: if, and only if, the entire region sequence matches this matcher's pattern。
相关文章推荐
- java正则表达式的匹配结果遍历方法find()和matches()不同
- java正则表达式的匹配结果遍历方法find()和matches()不同
- Java正则表达式之Matcher类的find和matches方法的区别
- java正则表达式方法的使用matches,find,lookingAt,replaceAll,group
- java调用webservice用是axis还是xfire的,两种调用方法不同
- Java在不同环境下获取当前路径的方法--this.getClass().getResource("")
- Java在不同环境下获取当前路径的方法--this.getClass().getResource("")
- Java在不同环境下获取当前路径的方法--this.getClass().getResource("")
- Java在不同环境下获取当前路径的方法--this.getClass().getResource("")
- 实例说明java中覆盖静态方法和非静态方法的不同
- java中正则表达式运用实例(用String类的matches方法演示)
- [转]java不同容器不同操作系统下中文问题解决方法
- Ant error: Could not find xml-commons-apis Java extension for this JVM解决方法
- Java/Flex使用相同密钥加密相同的信息,生成密文不同的解决方法
- 有关JAVA中不同方法对String连接性能的总结
- Java中Could not find the main class.program will exit和java.lang.UnsupportedClassVersionError 的解决方法 .
- java中两种不同的取日期时间方法
- 实例说明java中覆盖静态方法和非静态方法的不同
- Unable to find a javac compiler;Perhaps JAVA_HOME does not point to the JDK异常产生原因和解决方法
- javacript 刷新页面的不同方法