dcsimg
 

Submitted by: Octavia Anghel(octaviaanghel@gmail.com)

Wednesday Mar 6th 2019 by Octavia Anghel

Language: Java, Expertise: Intermediate - Learn how escape special characters in regular expressions.

In Java, characters as <([{\^-=$!|]})?*+.> cannot appear in a regular expression without being escaped.

For this, rely on the Pattern.quote() method as in the following example:

public static boolean contains(String t1, String t2) { 
return t1.matches(Pattern.quote(t2)); 
}

Now t2 can contain special characters.

Home
Mobile Site | Full Site