Java Data Type Tutorial - Java String.matches(String regex)








Syntax

String.matches(String regex) has the following syntax.

public boolean matches(String regex)

Example

In the following code shows how to use String.matches(String regex) method.

/*from   w w w. j  a v  a  2  s .c  o m*/
public class Main {

  public static void main(String[] args) {
  
    String str1 = "java2s", str2 = "java2s.com";
    
    boolean retval = str1.matches(str2);

    System.out.println("Value returned = " + retval);

    retval = str2.matches("com");

    System.out.println("Value returned = " + retval);    

    retval = str1.matches("java");

    System.out.println("Value returned = " + retval);
  }
}

The code above generates the following result.