Pattern matches(String ,CharSequence) method in Java with Examples Last Updated : 10 May, 2019 Comments Improve Suggest changes Like Article Like Report 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. Syntax: 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: Program 1: Java // Java program to demonstrate // Pattern.matches(String, CharSequence) method import java.util.regex.*; public class GFG { public static void main(String[] args) { // create a REGEX String String REGEX = "(.*)(ee)(.*)?"; // create the string // in which you want to search String actualString = "geeksforgeeks"; // use matches method to check the match boolean matcher = Pattern.matches(REGEX, actualString); // print values if match found if (matcher) { System.out.println("match found for Regex."); } else { System.out.println("No match found for Regex."); } } } Output: match found for Regex. Program 2: Java // Java program to demonstrate // Pattern.matches(String, CharSequence) method import java.util.regex.*; public class GFG { public static void main(String[] args) { // create a REGEX String String REGEX = "(.*)(welcome)(.*)?"; // create the string // in which you want to search String actualString = "The indian team wins worldcup"; // use matches() method to check the match boolean matcher = Pattern.matches(REGEX, actualString); // print values if match found if (matcher) { System.out.println("match found for Regex."); } else { System.out.println("No match found for Regex."); } } } Output: No match found for Regex. Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#matches(java.lang.String, java.lang.CharSequence) Comment More infoAdvertise with us Next Article Pattern matches(String ,CharSequence) method in Java with Examples A AmanSingh2210 Follow Improve Article Tags : Java Java-Functions Java 8 Java-Pattern Practice Tags : Java Similar Reads Pattern matcher(CharSequence) method in Java with Examples The matcher(CharSequence) method of the Pattern class used to generate a matcher that will helpful to match the given input as parameter to method against this pattern. The Pattern.matcher() method is very helpful when we need to check a pattern against a text a single time, and the default settings 2 min read Pattern split(CharSequence) method in Java with Examples split(CharSequence) method of a Pattern class used to splits the given char sequence passed as parameter to method around matches of this pattern.This method can split charSequence into an array of String's, using the regular expression used to compile the pattern as a delimiter.so we can say that t 2 min read Matcher reset(CharSequence) method in Java with Examples The reset(CharSequence input) method of Matcher Class is used to reset this matcher and insert the input String passed as the parameter to this matcher. Syntax: public Matcher reset(CharSequence input) Parameters: This method takes the parameter input which is the String to be inserted into matcher 2 min read Pattern split(CharSequence,int) method in Java with Examples split(CharSequence, int) method of a Pattern class used to splits the given char sequence passed as parameter to method around matches of this pattern.The array returned contains each substring of the input sequence created by this method. The substrings in the array are in the order in which they o 3 min read Matcher usePattern(Pattern) method in Java with Examples The usePattern() method of Matcher Class is used to get the pattern to be matched by this matcher. Syntax: public Matcher usePattern(Pattern newPattern) Parameters: This method takes a parameter newPattern which is the new pattern to be set. Return Value: This method returns a Matcher with the new P 2 min read String matches() Method in Java with Examples 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 fun 3 min read Matcher start(String) method in Java with Examples The start(String string) method of Matcher Class is used to get the start index of the match result already done, from the specified string. Syntax: public int start(String string) Parameters: This method takes a parameter string which is the String from which the start index of the matched pattern 2 min read Pattern compile(String) method in Java with Examples The compile(String) method of the Pattern class in Java is used to create a pattern from the regular expression passed as parameter to method. Whenever you need to match a text against a regular expression pattern more than one time, create a Pattern instance using the Pattern.compile() method.Synta 2 min read Pattern toString() Method in Java with Examples toString() method of a Pattern class used to return the string representation of this pattern. This return the regular expression from which this pattern was compiled. Syntax: public String toString() Parameters: This method accepts nothing as parameter. Return value: This method returns a string re 1 min read Like