Java matches() 方法
matches() 方法用于檢測(cè)字符串是否匹配給定的正則表達(dá)式。
調(diào)用此方法的 str.matches(regex) 形式與以下表達(dá)式產(chǎn)生的結(jié)果完全相同:
Pattern.matches(regex, str)
語(yǔ)法
public boolean matches(String regex)
參數(shù)
regex -- 匹配字符串的正則表達(dá)式。
返回值
在字符串匹配給定的正則表達(dá)式時(shí),返回 true。
實(shí)例
public class Test {
public static void main(String args[]) {
String Str = new String("www.o2fo.com");
System.out.print("返回值 :" );
System.out.println(Str.matches("(.*)youj(.*)"));
System.out.print("返回值 :" );
System.out.println(Str.matches("(.*)google(.*)"));
System.out.print("返回值 :" );
System.out.println(Str.matches("www(.*)"));
}
}
以上程序執(zhí)行結(jié)果為:
返回值 :false
返回值 :false
返回值 :true
更多建議: