Easy Tutorial
❮ Java8 Default Methods Collection Conversion ❯

Java matches() Method

Java String Class


The matches() method is used to check if the string matches the given regular expression.

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

Pattern.matches(regex, str)

Syntax

public boolean matches(String regex)

Parameters

Return Value

Returns true if the string matches the given regular expression.

Example

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

        System.out.print("Return Value :" );
        System.out.println(Str.matches("(.*)tutorialpro(.*)"));
        
        System.out.print("Return Value :" );
        System.out.println(Str.matches("(.*)google(.*)"));

        System.out.print("Return Value :" );
        System.out.println(Str.matches("www(.*)"));
    }
}

The output of the above program is:

Return Value :true
Return Value :false
Return Value :true

Java String Class

❮ Java8 Default Methods Collection Conversion ❯