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



Description

The java.util.regex.Pattern.toString() method returns the string representation of this pattern. This is the regular expression from which this pattern was compiled.

Declaration

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

public String toString()

Return Value

he string representation of this pattern.

Example

The following example shows the usage of java.util.regex.Pattern.toString() 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.toString());
   }
}

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

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