File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Making strings safe for regex" Watch "Making strings safe for regex" New topic
Author

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?

Cheers,

Steve
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42367
    
  64
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).


Ping & DNS - my free Android networking tools app
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: http://aspose.com/file-tools
 
subject: Making strings safe for regex