java.util.regex.Pattern.pattern() Method



Description

The java.util.regex.Pattern.pattern() method returns the regular expression from which this pattern was compiled.

Declaration

Following is the declaration for java.util.regex.Pattern.pattern() method.

public String pattern()

Return Value

The source of this pattern.

Example

The following example shows the usage of java.util.regex.Pattern.pattern() method.

package com.tutorialspoint;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PatternDemo {
   private static final String REGEX = "(.*)(\\d+)(.*)?# 3 capturing groups";
   private static final String INPUT = "This is a sample Text, 1234, with numbers in between.";

   public static void main(String[] args) {
      // create a pattern
      Pattern pattern = Pattern.compile(REGEX,Pattern.COMMENTS);
      
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT); 
      System.out.println(pattern.pattern());
   }
}

Let us compile and run the above program, this will produce the following result −

(.*)(\d+)(.*)?# 3 capturing groups
javaregex_pattern.htm
Advertisements