50

What's the correct regex for a plus character (+) as the first argument (i.e. the string to replace) to Java's replaceAll method in the String class? I can't get the syntax right.

John Topley
  • 113,588
  • 46
  • 195
  • 237

8 Answers8

98

You need to escape the + for the regular expression, using \.

However, Java uses a String parameter to construct regular expressions, which uses \ for its own escape sequences. So you have to escape the \ itself:

"\\+"
toolkit
  • 49,809
  • 17
  • 109
  • 135
  • 1
    I have the bad habit of using '/'s when building the regex and then running .replace('/', '\\'), so that I don't have to type "\\\\" to match a literal backslash. – Aaron Maenpaa Mar 04 '09 at 12:49
  • 3
    If you want to replace a fixed string, Pattern.quote(String) is a very nice friend. – gustafc Mar 04 '09 at 13:06
33

when in doubt, let java do the work for you:

myStr.replaceAll(Pattern.quote("+"), replaceStr);
james
  • 1,379
  • 7
  • 6
  • that's not sufficient. the replaceStr also needs to be quoted for replacement stuff. why not just use `myStr.replace("+", replaceStr)` – user102008 Jun 20 '11 at 09:41
  • 1
    Why would the replaceStr have to be quoted? String.replace() doesn't replace all occurrences. – FelixM May 17 '12 at 15:38
  • 1
    Pretty sure (not confirmed) that occurrences of `\n` where `n` is a digit will be replaced by the `n`th capture (which will be blank, seen as there are no captures). – Deco May 18 '12 at 05:50
14

You'll need to escape the + with a \ and because \ is itself a special character in Java strings you'll need to escape it with another \.

So your regex string will be defined as "\\+" in Java code.

I.e. this example:

String test = "ABCD+EFGH";
test = test.replaceAll("\\+", "-");
System.out.println(test);
Kris
  • 14,426
  • 7
  • 55
  • 65
4

Others have already stated the correct method of:

  1. Escaping the + as \\+
  2. Using the Pattern.quote method which escapes all the regex meta-characters.

Another method that you can use is to put the + in a character class. Many of the regex meta characters (., *, + among many others) are treated literally in the character class.

So you can also do:

orgStr.replaceAll("[+]",replaceStr);

Ideone Link

codaddict
  • 445,704
  • 82
  • 492
  • 529
2

If you want a simple string find-and-replace (i.e. you don't need regex), it may be simpler to use the StringUtils from Apache Commons, which would allow you to write:

mystr = StringUtils.replace(mystr, "+", "plus");
Simon Nickerson
  • 42,159
  • 20
  • 102
  • 127
2

Say you want to replace - with \\\-, use:

 text.replaceAll("-", "\\\\\\\\-");
bool.dev
  • 17,508
  • 5
  • 69
  • 93
Ramnath
  • 409
  • 5
  • 11
0
String str="Hello+Hello";   
str=str.replaceAll("\\+","-");
System.out.println(str);

OR

String str="Hello+Hello";   
str=str.replace(Pattern.quote(str),"_");
System.out.println(str);
Himanshu
  • 31,810
  • 31
  • 111
  • 133
JDGuide
  • 6,239
  • 12
  • 46
  • 64
0

How about replacing multiple ‘+’ with an undefined amount of repeats?

Example: test+test+test+1234

(+) or [+] seem to pick on a single literal character but on repeats.

Jay
  • 1