- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Role of Matcher.matches() method in Java Regular Expressions
The method java.time.Matcher.matches() matches the given region against the specified pattern. It returns true if the region sequence matches the pattern of the Matcher and false otherwise.
A program that demonstrates the method Matcher.matches() in Java regular expressions is given as follows −
Example
import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { Pattern p = Pattern.compile("Apple"); String str1 = "apple"; String str2 = "Apple"; String str3 = "APPLE"; Matcher m1 = p.matcher(str1); Matcher m2 = p.matcher(str2); Matcher m3 = p.matcher(str3); System.out.println(str1 + " matches? " + m1.matches()); System.out.println(str2 + " matches? " + m2.matches()); System.out.println(str3 + " matches? " + m3.matches()); } }
Output
The output of the above program is as follows −
apple matches? false Apple matches? true APPLE matches? false
Now let us understand the above program.
Various Matcher patterns are compared with the original pattern “Apple” using the matches() method and the values returned is printed. A code snippet which demonstrates this is as follows −
Pattern p = Pattern.compile("Apple"); String str1 = "apple"; String str2 = "Apple"; String str3 = "APPLE"; Matcher m1 = p.matcher(str1); Matcher m2 = p.matcher(str2); Matcher m3 = p.matcher(str3); System.out.println(str1 + " matches? " + m1.matches()); System.out.println(str2 + " matches? " + m2.matches()); System.out.println(str3 + " matches? " + m3.matches());
- Related Articles
- Role of Matcher.group() method in Java Regular Expressions
- Role of Matcher.find(int) method in Java Regular Expressions
- Reset the Matcher in Java Regular Expressions
- Matcher matches() method in Java with Examples
- Getting the list of all the matches Java regular expressions
- Pattern.matches() method in Java Regular Expressions
- Matcher.pattern() method in Java Regular Expressions
- Looping over matches with JavaScript regular expressions
- Working with Matcher.start() method in Java Regular Expressions
- Working with Matcher.end() method in Java Regular Expressions
- What is the role of special characters in JavaScript Regular Expressions?
- Regular expression matches multiple lines in Java
- Java Regular Expressions Tutorial
- How to find the subsequence in an input sequence that matches the pattern required in Java Regular Expressions?
- Greedy quantifiers Java Regular expressions in java.

Advertisements