The matches(String, CharSequence) method of the Pattern class in Java is used to answer whether or not the regular expression matches on the input. To do so we compile the given regular expression and attempts to match the given input against it where both regular expression and input passed as a parameter to the method. If a pattern is to be used multiple times, compiling it once and reusing it will be more efficient than invoking this method each time.
public static boolean matches(String regex, CharSequence input)
Parameters: This method accepts two parameters:
- regex: This parameter represents the expression to be compiled.
- input: The character sequence to be matched.
Return Value: This method returns a boolean value, answering whether or not the regular expression matches on the input.
Below programs illustrate the matches(String, CharSequence) method:
match found for Regex.
No match found for Regex.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.