Escape Characters in Regular Expressions

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.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist