views:

2304

answers:

6

In Java, suppose I have a String variable S, and I want to search for it inside of another String T, like so:

   if (T.matches(S)) ...

(note: the above line was T.contains() until a few posts pointed out that that method does not use regexes. My bad.)

But now suppose S may have unsavory characters in it. For instance, let S = "[hi". The left square bracket is going to cause the regex to fail. Is there a function I can call to escape S so that this doesn't happen? In this particular case, I would like it to be transformed to "\[hi".

+2  A: 

Any particular reason not to use String.indexOf() instead? That way it will always be interpreted as a regular string rather than a regex.

Jay
Since Java 1.5 there is String#contains(...), which results in more readable code than indexOf(...).
Fabian Steeg
+11  A: 

String.contains does not use regex, so there isn't a problem in this case.

Where a regex is required, rather rejecting strings with regex special characters, use java.util.regex.Pattern.quote to escape them.

Tom Hawtin - tackline
A: 

Regex uses the backslash character '\' to escape a literal. Given that java also uses the backslash character you would need to use a double bashslash like:

   String S = "\\[hi"

That will become the String:

  \[hi

which will be passed to the regex.

Or if you only care about a literal String and don't need a regex you could do the following:

if (T.indexOf("[hi") != -1)  {
Aaron
A: 

T.contains() (according to javadoc : http://java.sun.com/javase/6/docs/api/java/lang/String.html) does not use regexes. contains() delegates to indexOf() only.

So, there are NO regexes used here. Were you thinking of some other String method ?

anjanb
+6  A: 

As Tom Hawtin said, you need to quote the pattern. You can do this in two ways (edit: actually three ways, as pointed out by @diastrophism):

  1. Surround the string with "\Q" and "\E", like:

    if (T.matches("\Q" + S + "\E"))
    
  2. Use Pattern instead. The code would be something like this:

    Pattern sPattern = Pattern.compile(S, Pattern.LITERAL);
    if (sPattern.matcher(T).matches()) { /* do something */ }
    

    This way, you can cache the compiled Pattern and reuse it. If you are using the same regex more than once, you almost certainly want to do it this way.

Note that if you are using regular expressions to test whether a string is inside a larger string, you should put .* at the start and end of the expression. But this will not work if you are quoting the pattern, since it will then be looking for actual dots. So, are you absolutely certain you want to be using regular expressions?

Michael Myers
+2  A: 

Try Pattern.quote(String). It will fix up anything that has special meaning in the string.

Diastrophism