Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

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

 Live Demo

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());
Updated on: 2020-06-30T08:05:47+05:30

141 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements