It's not a secret anymore!
The moose likes Java in General and the fly likes Making strings safe for regex Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Making strings safe for regex" Watch "Making strings safe for regex" New topic

Making strings safe for regex

Steve Wood
Ranch Hand

Joined: Jan 08, 2003
Posts: 137
Hi guys,

Is there a way to make strings safe for regex. What I mean is:

We have an application that uses regex to do some pattern finding. However, we don't want anything the user enters to interfere with this pattern finding.

So, what's happening is that some of the characters used by the user are creating regex searches (and throwing errors).

Is there a way to convert all regex special characters to safe versions? Or convert a subset?

Specifically, how do you convert a $ symbol to a safe version?


Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
All of the metacharacters need to be escaped by preceding them with a backslash. Some of those are ^ $ . [ ] { } ( ) * +

Note that inside of classes (i.e., inside [ ]), a slightly different set applies (e.g., "-" is special while "." is not).
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
If you're running Java 5, you can either escape the string with the static Pattern.quote(String) method, or compile it with the Pattern.LITERAL flag set. For earlier releases, you can just escape all punctuation characters:
I agree. Here's the link:
subject: Making strings safe for regex
jQuery in Action, 3rd edition