Java Program to Illustrate Escaping Characters in Regex - GeeksforGeeks

30 Sep, 2021
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:
Using \Q and \E for escapingUsing backslash(\\) for escaping
Using \Q and \E for escaping
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.
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 classesimport java.io.*;import java.util.regex.*; // Main classclass 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());    }}
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
gabaa406
anikaseth98
java-regular-expression
Picked
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Exceptions in Java
Stream In Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Convert a String to Character array in Java
Java Programming Examples
How to Iterate HashMap in Java?
Implementing a Linked List in Java using Class
Min Heap in Java