java的lookingat_Java Matcher.lookingAt()部分匹配字符串
首頁?>?基礎教程?>?正則表達式?>?Matcher類
Java Matcher.lookingAt()部分匹配字符串
Matcher.lookingAt()對前面的字符串進行匹配,只有匹配到的字符串在最前面才返回true。
定義
public boolean lookingAt()
例子
實例一
Pattern p=Pattern.compile("\\d+");
Matcher m=p.matcher("22bb23");
m.lookingAt();//返回true,因為\d+匹配到了前面的22
Matcher m2=p.matcher("aa2223");
m2.lookingAt();//返回false,因為\d+不能匹配前面的aa
實例二
public static void main(String arg[]) {
String string = "123-34345-234-00";
Pattern pattern = Pattern.compile("\\d{3,5}");
Matcher matcher = pattern.matcher(string);
boolean result1 = matcher.matches();
System.out.println("result1=" + result1);
//重置匹配器。
matcher.reset();
//嘗試查找與該模式匹配的輸入序列的下一個子序列。
boolean result2 = matcher.find();
System.out.println("result2=" + result2);
boolean result3 = matcher.find();
System.out.println("result3=" + result3);
boolean result4 = matcher.find();
System.out.println("result4=" + result4);
boolean result5 = matcher.find();
System.out.println("result5=" + result5);
//嘗試將從區域開頭開始的輸入序列與該模式匹配。
boolean result6 = matcher.lookingAt();
System.out.println("result6=" + result6);
boolean result7 = matcher.lookingAt();
System.out.println("result7=" + result7);
boolean result8 = matcher.lookingAt();
System.out.println("result8=" + result8);
}
運行結果:
result1=false
result2=true
result3=true
result4=true
result5=false
result6=true
result7=true
result8=true
總結
lookingAt是部分匹配,總是從第一個字符進行匹配,匹配成功了不再繼續匹配,匹配失敗了,也不繼續匹配。
版權聲明:本文為JAVASCHOOL原創文章,未經本站允許不得轉載。
總結
以上是生活随笔為你收集整理的java的lookingat_Java Matcher.lookingAt()部分匹配字符串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java script jquery_J
- 下一篇: mysql 删除数据后myd_Windo