
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Matcher Pattern Method in Java Regular Expressions
The method java.time.Matcher.pattern() returns the pattern that is matched upon by the Matcher. This method accepts no parameters.
A program that demonstrates the method Matcher.pattern() 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[]) { String regex = "Apple"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher("AppleIsAFruit"); System.out.println("Pattern: " + m.pattern()); } }
The output of the above program is as follows −
Pattern: Apple
Now let us understand the above program.
The pattern that is matched upon by the Matcher is returned using the Matcher.pattern() method and then printed. A code snippet which demonstrates this is as follows −
String regex = "Apple"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher("AppleIsAFruit"); System.out.println("Pattern: " + m.pattern());
Advertisements