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



Description

The java.util.regex.Matcher.toString() method returns the string representation of this matcher. The string representation of a Matcher contains information that may be useful for debugging. The exact format is unspecified.

Declaration

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

public MatchResult toString()

Return Value

The string representation of this matcher.

Example

The following example shows the usage of java.util.regex.Matcher.toString() method.

package com.tutorialspoint;

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

public class MatcherDemo {
   private static final String REGEX = "(.*)(\\d+)(.*)";
   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);
      
      // get a matcher object
      Matcher matcher = pattern.matcher(INPUT); 
      System.out.println(matcher.toString());
   }
}

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

java.util.regex.Matcher[pattern=(.*)(\d+)(.*) region=0,53 lastmatch=]
javaregex_matcher.htm
Advertisements