Pattern quote(String) method in Java with Examples

quote(String) method of a Pattern class used to returns a literal pattern String for the specified String passed as parameter to method.This method produces a String equivalent to s that can be used to create a Pattern. Metacharacters or escape sequences in the input sequence will be given no special meaning. If you compile the value returned by the quote method, you’ll get a Pattern which matches the literal string that you passed as a parameter to method.\Q and \E mark the beginning and end of the quoted part of the string.

Syntax:

public static String quote(String s)

Parameters: This method accepts a single parameter s which represents the string to be literalized.

Return value: This method returns a literal string replacement for String s.

Below programs illustrate the quote() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.quote() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "ee";
  
        // create the string
        // in which you want to search
        String actualString
            = "geeksforgeeks";
  
        // create equivalent String for REGEX
        String eqREGEX = Pattern.quote(REGEX);
  
        // create a Pattern using eqREGEX
        Pattern pattern = Pattern.compile(eqREGEX);
  
        // get a matcher object
        Matcher matcher = pattern.matcher(actualString);
  
        // print values if match found
        boolean matchfound = false;
        while (matcher.find()) {
            System.out.println("found the Regex in text:"
                               + matcher.group()
                               + " starting index:"
                               + matcher.start()
                               + " and ending index:"
                               + matcher.end());
  
            matchfound = true;
        }
        if (!matchfound) {
            System.out.println("No match found for Regex.");
        }
    }
}

chevron_right


Output:

found the Regex in text:ee starting index:1 and ending index:3
found the Regex in text:ee starting index:9 and ending index:11

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// Pattern.quote() method
  
import java.util.regex.*;
  
public class GFG {
    public static void main(String[] args)
    {
        // create a REGEX String
        String REGEX = "welcome";
  
        // create the string
        // in which you want to search
        String actualString
            = "welcome to jungle";
  
        // create equivalent String for REGEX
        String eqREGEX = Pattern.quote(REGEX);
  
        // create a Pattern using eqREGEX
        Pattern pattern = Pattern.compile(eqREGEX);
  
        // get a matcher object
        Matcher matcher = pattern.matcher(actualString);
  
        // print values if match found
        boolean matchfound = false;
        while (matcher.find()) {
            System.out.println("match found");
            matchfound = true;
        }
        if (!matchfound) {
            System.out.println("No match found");
        }
    }
}

chevron_right


Output:

match found

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



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.