Java matches() 方法
matches() 方法用於檢測字串是否匹配給定的正則運算式。
調用此方法的 str.matches(regex) 形式與以下運算式產生的結果完全相同:
Pattern.matches(regex, str)
語法
public boolean matches(String regex)
參數
regex -- 匹配字串的正則運算式。
返回值
在字串匹配給定的正則運算式時,返回 true。
實例
public class Test {
public static void main(String args[]) {
String Str = new String("www.xuhuhu.com");
System.out.print("返回值 :" );
System.out.println(Str.matches("(.*)zaixian(.*)"));
System.out.print("返回值 :" );
System.out.println(Str.matches("(.*)google(.*)"));
System.out.print("返回值 :" );
System.out.println(Str.matches("www(.*)"));
}
}
以上程式執行結果為:
返回值 :true 返回值 :false 返回值 :true

Java String類