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.example.com");
System.out.print("返回值 :" );
System.out.println(Str.matches("(.*)サンプル(.*)"));
System.out.print("返回值 :" );
System.out.println(Str.matches("(.*)google(.*)"));
System.out.print("返回值 :" );
System.out.println(Str.matches("www(.*)"));
}
}
上記のプログラムの実行結果は次のとおりです:
返回值 :true 返回值 :false 返回值 :true
その他の拡張機能
Java Stringクラス