Java Basics:String Methods-4
Java String matches() The Java String matches() method checks whether the string matches the given regular expression or not. The syntax of the string matches() method is: string.matches(String regex) Here, string is an object of the String class. matches() Parameters The matches() method takes a single parameter. regex - a regular expression matches() Return Value returns true if the regex matches the string returns false if the regex doesn't match the string Example 1: Java matches() class Main { public static void main (String[] args) { // a regex pattern for // five letter string that starts with 'a' and end with 's' String regex = "^a...s$" ; System.out.println( "abs" .matches(regex)); // false System.out.println( "alias" .matches(regex)); // true System.out.println( "an abacus" .matches(regex)); // false System.out.println( "abyss" .matches(regex)); // tru...