Java matches() 方法說明

boolean matches(String regex):
matches() 方法用於檢測字符串是否匹配給定的正則表達式。

調用此方法的 str.matches(regex) 形式與以下表達式產生的結果完全相同:

Pattern.matches(regex, str)

語法:

public boolean matches(String regex)

參數:
regex – 匹配字符串的正則表達式。

返回值:
在字符串匹配給定的正則表達式時,返回 true。

實例:

public class Demo1 {
    public static void main(String[] args){
        String Str = new String("我是一隻小小鳥");

        System.out.println("返回值 :"+Str.matches("(.*)一隻(.*)"));

        boolean flag = Str.matches("我是(.*)");
        if(flag){
            System.out.println("我被重新定義爲:HelloWord!");
        }else{
            System.out.println("我沒有被重新定義!");
        }
    }
}

以上程序執行結果爲:

返回值 :true
我被重新定義爲:HelloWord!

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章