Java String matches() Method

Java String matches() Method | The matches() method is used checks whether the given string matches the substring or not. It basically works on the regular expression. We give some regular expressions as a parameter to the matches method and it checks for the same word in the string.

Note:- In the regular expression ‘.’ represents any/all characters, ‘^’ represents starting of the string and ‘$’ represents the ending of the string, ‘+’ means one or more times. See the below examples for String Java matches():-

Example-1:-
String1 = “APply”
String2 = “AP..y”
True. String1 starts with “AP”, ends with “y”, and has two characters in-between “AP” and “y”.

Example-2:-
String1 = “KP”
String2 = “^K”
True. The string1 starts with ‘K’.

Example-3:-
String1 = “Man”
String2 = “n$”
True. The string1 ends with ‘n’.

Example-4:-
String1 = “Monkey”
String2 = “^o”
False. The string1 starts with ‘M’ but not with ‘o’, therefore matches() method returns false.

The syntax for the matches method is as follows:-
public boolean matches(String regex)
Parameters: regex, the regular expression which needs to be matched.
Return type: boolean
Returns: true, if the regular expression matches, or else returns false if the regular expression is not found.

Java String matches() Method Example

public class Main {
   public static void main(String[] args) {
      String str = "^J..a$";
      System.out.println("Java".matches(str));
   }
}

Output:-

true

In the above string, we are searching for the character ‘J’ in the first and character ‘a’ in the last. There can be any two characters in between ‘J’ and ‘a’. Hence the given string matches the regular expression.

Java Matching Strings

public class Main {
   public static void main(String args[]) {
      String string = new String("Java Programming Language");
      System.out.print("Does string contains regex:- (.*)Java(.*)?: ");
      System.out.println(string.matches("(.*)Java(.*)"));
   }
}

Output:-

Does string contains regex (.*)Java(.*) ? : true

It will check whether the string contains “Java” or not? There can be any character any number of times before or after the “Java” sub-string. The given string matches this regular expression therefore it gives the result true.

Java Match Character In String

public class Main {
   public static void main(String[] args) {
      String str = "^[1-9]+$";
      System.out.println("a123".matches(str));
      System.out.println("98416".matches(str));
      System.out.println("98 41".matches(str));
   }
}

Output:-

false
true
false

The given regular expression checks whether the string contains only digits 0-9 or not. The first and third-string don’t match the regular expression therefore matches() return false for them. Whereas for the second string matches() method returns true.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.