Java.lang.String.matches() Method
Advertisements
Description
The java.lang.String.matches() method tells whether or not this string matches the given regular expression.
Declaration
Following is the declaration for java.lang.String.matches() method
public boolean matches(String regex)
Parameters
regex -- This is the regular expression to which this string is to be matched.
Return Value
This method returns true if, and only if, this string matches the given regular expression.
Exception
PatternSyntaxException -- if the regular expression's syntax is invalid.
Example
The following example shows the usage of java.lang.String.matches() method.
package com.tutorialspoint;
import java.lang.*;
public class StringDemo {
public static void main(String[] args) {
String str1 = "tutorials", str2 = "learning";
boolean retval = str1.matches(str2);
// method gets different values therefore it returns false
System.out.println("Value returned = " + retval);
retval = str2.matches("learning");
// method gets same values therefore it returns true
System.out.println("Value returned = " + retval);
retval = str1.matches("tuts");
// method gets different values therefore it returns false
System.out.println("Value returned = " + retval);
}
}
Let us compile and run the above program, this will produce the following result:
Value returned = false Value returned = true Value returned = false