Escape 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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Related Posts