Pattern toString() Method in Java with Examples

toString() method of a Pattern class used to return the string representation of this pattern. This return the regular expression from which this pattern was compiled.

Syntax:

public String toString()

Parameters: This method accepts nothing as parameter.



Return value: This method returns a string representation of this pattern.

Below programs illustrate the toString() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.toString() method
  
import java.util.regex.*;
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "geeks";
  
        // create a Pattern using REGEX
        Pattern pattern = Pattern.compile(REGEX);
  
        // print pattern
        System.out.println("Pattern:" + pattern);
    }
}

chevron_right


Output:

Pattern:geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.toString() method
  
import java.util.regex.*;
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "[* & # $ !]+\\S";
  
        // create a Pattern using REGEX
        Pattern pattern = Pattern.compile(REGEX);
  
        // print pattern
        System.out.println("Pattern:" + pattern);
    }
}

chevron_right


Output:

Pattern:[* & # $ !]+\S

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#toString()



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.