- 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
Pattern.matches() method in Java Regular Expressions
The java.util.regex.Pattern.matches() method matches the regular expression and the given input. It has two parameters i.e. the regex and the input. It returns true if the regex and the input match and false otherwise.
A program that demonstrates the method Pattern.matches() in Java regular expressions is given as follows:
Example
import java.util.regex.Pattern; public class Demo { public static void main(String args[]) { String regex = "a*b"; String input = "aaab"; System.out.println("Regex: " + regex); System.out.println("Input: " + input); boolean match = Pattern.matches(regex, input); System.out.println("
Does the regex match with the input? " + match); } }
Output
Regex: a*b Input: aaab Does the regex match with the input? true
Now let us understand the above program.
The regex and the input values are printed. Then the Pattern.matches() method matches the regular expression and the given input and the result is printed. A code snippet which demonstrates this is as follows:
String regex = "a*b"; String input = "aaab"; System.out.println("Regex: " + regex); System.out.println("Input: " + input); boolean match = Pattern.matches(regex, input); System.out.println("
Does the regex match with the input? " + match);
- Related Articles
- Pattern matches() method in Java with examples
- How to find the subsequence in an input sequence that matches the pattern required in Java Regular Expressions?
- Getting the list of all the matches 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
- Role of Matcher.matches() method in Java Regular Expressions
- Role of Matcher.group() method in Java Regular Expressions
- Which package is used for pattern matching with regular expressions in java?
- Role of Matcher.find(int) method in Java Regular Expressions
- Regular expression matches multiple lines in Java
- Java Regular Expressions Tutorial
- Greedy quantifiers Java Regular expressions in java.
- Back references in Java regular expressions

Advertisements