![]() |
VOOZH | about |
In Java, the matches() method in the String class checks if a string matches a specified regular expression. It is useful for validating input patterns and searching within strings.
In this article, we will learn how to use the matches() method effectively in Java with examples to illustrate its functionality.
Example:
In this example, we will check if a string contains only digits.
true
Table of Content
boolean matches(String regex)
Parameter:
Return Type:
true if the string matches the regex, otherwise false.matches() MethodWe will use matches() method to check if a string contains only alphabetic characters.
true
To validate an email addresses, regular expressions can be used with matches() method.
true
To check whether a string follows a specific pattern like combination of digits or letters, we can use matches() method of String class.
true
matches() method is an efficient way to check if a string is in a specified pattern.