Java Program to Illustrate Escaping Characters in Regex
Last Updated :
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 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.
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
import java.io.*;
import java.util.regex.*;
class GFG {
public static void main(String[] args)
{
String s = "Geeks.forGeeks" ;
String s1 = "GeeksforGeeks" ;
Pattern p = Pattern.compile( "\\Q.\\E" );
Pattern p1 = Pattern.compile( "." );
Matcher m = p.matcher(s);
Matcher m1 = p.matcher(s1);
Matcher m2 = p1.matcher(s);
Matcher m3 = p1.matcher(s1);
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
import java.io.*;
import java.util.regex.*;
class GFG {
public static void main (String[] args) {
String s= "Geeks.forGeeks" ;
String s1= "GeeksforGeeks" ;
Pattern p=Pattern.compile( "\\." );
Pattern p1=Pattern.compile( "." );
Matcher m=p.matcher(s);
Matcher m1=p.matcher(s1);
Matcher m2=p1.matcher(s);
Matcher m3=p1.matcher(s1);
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
Share your thoughts in the comments
Please Login to comment...