Java matches() Method

Java String类Java String Class


The matches() method is used to check whether a string matches a given regular expression.

Calling this method in the form str.matches(regex) produces exactly the same result as the following expression:

Pattern.matches(regex, str)

Syntax

public boolean matches(String regex)

Parameters

  • regex-- the regular expression to match the string 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.example.com");

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

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

The output of the above program is:

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

Java String类Java String Class

Other Extensions