String类的matches()使用
matches()方法为全局匹配,如果正则能把整个字符串匹配上则返回true,否则返回false
String value4 = "11111111112";
if (value4.matches("\\d+")) {
System.out.println("matches匹配成功");
}else{
System.out.println("matches匹配失败");
}
String value5 = "11111111112QWE";
if (value5.matches("\\d+")) {
System.out.println("matches匹配成功");
}else{
System.out.println("matches匹配失败");
}
运行结果:
matches匹配成功
matches匹配失败
Matcher类的matches()使用
matches()方法也为全局匹配,使用方式和String的matches()相似,具体不同点从网上查询到有性能方面差异,Matcher类的matches()方法较快。(必须全局匹配)
String value2 = "123456789";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(value2);
if (m.matches()) {
System.out.println("matches匹配到的内容:" + m.group());
System.out.println("matches匹配成功");
} else {
System.out.println("matches匹配失败");
}
运行结果:
matches匹配到的内容:123456789
matches匹配成功
Matcher类的find()使用
find()方法是局部匹配,如果匹配成功则返回true,如果find方法调用第二次,则匹配的位置会继续向下匹配。(不可用全局匹配)
String value = "123AAA456789BBBC";
Pattern pattern = Pattern.compile("[1-9]\\d{2}");
Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}else {
System.out.println("find匹配失败");
}
运行结果:
find匹配到的内容:123
find开始位置:0 →→→→→→→→ 结束位置:3
find匹配到的内容:456
find开始位置:6 →→→→→→→→ 结束位置:9
find匹配到的内容:789
find开始位置:9 →→→→→→→→ 结束位置:12
find匹配失败
方法小结:
通过运行结果可以看出每次调用find()方法,匹配的初始位置都会向后移,也就是说匹配到的内容不会再匹配第二次
String value2 = "123456789";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(value2);
if(m.find()){
System.out.println("find匹配到的内容:" + m.group());
System.out.println("find匹配成功");
}else {
System.out.println("find匹配失败");
}
运行结果:
find匹配失败
方法小结:
find()方法即使正则书写正确,全局匹配也返回false。
Matcher类的lookingAt()使用
lookingAt()方法是从字符串的初始位置开始匹配,无论匹配多少次,无论是否匹配成功,都从初始位置开始匹配(可以全局匹配)
String value3 = "2222AAAA33333";
Pattern pattern1 = Pattern.compile("[1-9]\\d{3}");
Matcher matcher1 = pattern1.matcher(value3);
Pattern pattern2 = Pattern.compile("[1-9]\\d{4}");
Matcher matcher2 = pattern2.matcher(value3);
if (matcher1.lookingAt()) {
System.out.println("lookingAt匹配到的内容:" + matcher1.group());
System.out.println("lookingAt匹配成功");
}else {
System.out.println("lookingAt匹配失败");
}
if (matcher1.lookingAt()) {
System.out.println("lookingAt匹配到的内容:" + matcher1.group());
System.out.println("lookingAt匹配成功");
}else {
System.out.println("lookingAt匹配失败");
}
if (matcher2.lookingAt()) {
System.out.println("lookingAt匹配到的内容:" + matcher2.group());
System.out.println("lookingAt匹配成功");
}else {
System.out.println("lookingAt匹配失败");
}
运行结果:
lookingAt匹配到的内容:2222
lookingAt匹配成功
lookingAt匹配到的内容:2222
lookingAt匹配成功
lookingAt匹配失败
方法小结:
lookingAt()方法,无论匹配多少次,都只从字符串的初始位置开始匹配。
如果初始位置匹配不到,无论后面是否匹配到,都返回false
String value2 = "123456789";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(value2);
` if (m.lookingAt()) {
System.out.println("lookingAt匹配到的内容:" + m.group());
System.out.println("lookingAt匹配成功");
}else {
System.out.println("lookingAt匹配失败");
}
运行结果:
lookingAt匹配到的内容:123456789
lookingAt匹配成功
方法小结:
lookingAt()方法可以全局匹配。
Matcher类的reset()使用
reset()方法是重置作用,重置此匹配器,返回一个当前匹配器。
String value = "123AAA456789BBBC";
Pattern pattern = Pattern.compile("[1-9]\\d{2}");
Matcher matcher = pattern.matcher(value);
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}else {
System.out.println("find匹配失败");
}
if (matcher.lookingAt()) {
System.out.println("lookingAt匹配到的内容:" + matcher.group());
System.out.println("lookingAt开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
matcher.reset();
System.out.println("匹配器被重置");
if (matcher.find()) {
System.out.println("find匹配到的内容:" + matcher.group());
System.out.println("find开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
if (matcher.lookingAt()) {
System.out.println("lookingAt匹配到的内容:" + matcher.group());
System.out.println("lookingAt开始位置:" + matcher.start() + " →→→→→→→→ 结束位置:" + matcher.end());
}
运行结果:
find匹配到的内容:123
find开始位置:0 →→→→→→→→ 结束位置:3
find匹配到的内容:456
find开始位置:6 →→→→→→→→ 结束位置:9
find匹配到的内容:789
find开始位置:9 →→→→→→→→ 结束位置:12
find匹配失败
lookingAt匹配到的内容:123
lookingAt开始位置:0 →→→→→→→→ 结束位置:3
匹配器被重置
find匹配到的内容:123
find开始位置:0 →→→→→→→→ 结束位置:3
lookingAt匹配到的内容:123
lookingAt开始位置:0 →→→→→→→→ 结束位置:3
matches匹配到的内容:123456789
方法小结:
通过上述代码运行结果可以看出,reset()重置后,find()方法的查找又从最初位置开始匹配,而lookingAt()方法本就一直从最初位置匹配,所以结果不受影响,同理,matches()方法是全局匹配,所以应该也不会受影响。
附言
新年新气象,第一次总结技术文档,大牛勿喷,如有错误,欢迎指摘,随时更新,新人小白,共同成长。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)