Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java matches() Method

Java matches() Method

Java String ClassJava String class


The matches() method is used to test whether the string matches the given regular expression.

The str.matches(regex) form of calling this method produces exactly the same result as the following expression:


Pattern.matches(regex, str)

Syntax


public boolean matches(String regex)

Parameters

Return Value

Returns true when the string matches the given regular expression.

Example


public class Test {
    public static void main(String args[]) {
        String Str = new String("www.runoob.com");

        System.out.print("返回值 :" );
        System.out.println(Str.matches("(.*)runoob(.*)"));
        
        System.out.print("返回值 :" );
        System.out.println(Str.matches("(.*)google(.*)"));

        System.out.print("返回值 :" );
        System.out.println(Str.matches("www(.*)"));
    }
}

The execution result of the above program is:


返回值 :true
返回值 :false
返回值 :true

Java String ClassJava String class