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.matches() method is very helpful when we need to check a pattern against a text a single time, and the default settings of the Pattern class are appropriate.
Syntax:
public Matcher matcher(CharSequence input)
Parameters: This method accepts a single parameter input which represents the character sequence to be matched.
Return Value: This method returns a new matcher for this pattern.
Below programs illustrate the matcher(CharSequence) method:
Program 1:
// Java program to demonstrate // Pattern.matcher(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"; // create a Pattern Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(actualString); // print values if match found boolean matchfound = false; while (matcher.find()) { System.out.println("found the Regex in text:" + matcher.group() + " starting index:" + matcher.start() + " and ending index:" + matcher.end()); matchfound = true; } if (!matchfound) { System.out.println("No match found for Regex."); } } } |
found the Regex in text:geeksforgeeks starting index:0 and ending index:13
Program 2:
// Java program to demonstrate // Pattern.matcher(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 = "geeksforgeeks"; // create a Pattern Pattern pattern = Pattern.compile(REGEX); // get a matcher object Matcher matcher = pattern.matcher(actualString); // print values if match found boolean matchfound = false; while (matcher.find()) { System.out.println("match found"); matchfound = true; } if (!matchfound) { System.out.println("No match found"); } } } |
No match found
References:
https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#matcher(java.lang.CharSequence)
Recommended Posts:
- Pattern pattern() method in Java with Examples
- Pattern asPredicate() Method in Java with Examples
- Pattern splitAsStream() Method in Java with Examples
- Matcher pattern() method in Java with Examples
- Pattern toString() Method in Java with Examples
- Pattern flags() method in Java with Examples
- Pattern split(CharSequence,int) method in Java with Examples
- Pattern compile(String) method in Java with Examples
- Pattern quote(String) method in Java with Examples
- Pattern split(CharSequence) method in Java with Examples
- Pattern compile(String,int) method in Java with Examples
- Matcher usePattern(Pattern) method in Java with Examples
- Pattern matches(String ,CharSequence) method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh



