Open In App

Java Program to Illustrate Escaping Characters in Regex

Last Updated : 30 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Special Characters like dot(.), hash(#), etc., which have a special meaning to the regular expression need to be escaped to match in the regular expression. For example, if dot(.) is not escaped in a regular expression, it matches any single character, thus giving ambiguous results.

Methods: 

Characters can be escaped in Java Regex in two ways which are listed as follows which we will be discussing upto depth:

  1. Using \Q and \E for escaping
  2. Using backslash(\\) for escaping

Method 1: Using \Q and \E for escaping

  • We can use the \Q and \E escape sequences to escape characters.
  • \Q marks the start of the escape sequence whereas \E marks the end of the escape sequence.
  • Any characters between the \Q and \E are escaped.
  • Generally used for escaping multiple characters.

Implementation:

In the below source code the Regex pattern p is escaped for the dot(.) operator, whereas the pattern p1 is not escaped for dot(.). Thus, the pattern p matches only with the string s whereas the pattern p1 matches with both the strings s and s1.

Example

Java




// Java Program to Illustrate Escaping Characters in Java
// Regex Using \Q and \E for escaping
 
// Importing required classes
import java.io.*;
import java.util.regex.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Sample strings as inputs
        String s = "Geeks.forGeeks";
        String s1 = "GeeksforGeeks";
 
        // Creating object of Pattern class
 
        // 1. Patterns with dot escaped
        Pattern p = Pattern.compile("\\Q.\\E");
 
        // 2, Pattern without dot escaped
        Pattern p1 = Pattern.compile(".");
 
        // Matchers for each pattern string combination
        Matcher m = p.matcher(s);
        Matcher m1 = p.matcher(s1);
        Matcher m2 = p1.matcher(s);
        Matcher m3 = p1.matcher(s1);
 
        // Print and display whether p,p1 matches
        // or not via boolean true false
        System.out.println("p matches s: " + m.find());
        System.out.println("p matches s1: " + m1.find());
        System.out.println("p1 matches s: " + m2.find());
        System.out.println("p1 matches s1: " + m3.find());
    }
}


Output

p matches s: true
p matches s1: false
p1 matches s: true
p1 matches s1: true

Method 2: Using backslash(\\) for escaping

  • We can use a backslash to escape characters.
  • We require two backslashes as backslash is itself a character and needs to be escaped.
  • Characters after \\ are escaped.
  • It is generally used to escape characters at the end of the string.

Implementation:

In the below source code the Regex pattern p is escaped for the dot(.) operator, whereas the pattern p1 is not escaped for dot(.). Thus the pattern p matches only with the string s whereas the pattern p1 matches with both the strings s and s1.

Example:

Java




/*package whatever //do not write package name here */
 
import java.io.*;
import java.util.regex.*;
 
class GFG {
    public static void main (String[] args) {
         
         
        String s="Geeks.forGeeks";//sample strings
        String s1="GeeksforGeeks";
          //patterns with dot escaped
        Pattern p=Pattern.compile("\\.");
          // pattern without dot escaped
          Pattern p1=Pattern.compile(".");
          //matchers for each pattern string combination
        Matcher m=p.matcher(s);
          Matcher m1=p.matcher(s1);
          Matcher m2=p1.matcher(s);
          Matcher m3=p1.matcher(s1);
          //outputs
        System.out.println("p matches s: "+m.find());
          System.out.println("p matches s1: "+m1.find());
          System.out.println("p1 matches s: "+m2.find());
          System.out.println("p1 matches s1: "+m3.find());
    }
}


Output:

p matches s: true
p matches s1: false
p1 matches s: true
p1 matches s1: true


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads