Java matches() Method
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
-
regex -- The regular expression that the string is matched against.
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 class