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.

- 113,588
- 46
- 195
- 237
8 Answers
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:
"\\+"

- 49,809
- 17
- 109
- 135
-
1I 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
-
3If you want to replace a fixed string, Pattern.quote(String) is a very nice friend. – gustafc Mar 04 '09 at 13:06
when in doubt, let java do the work for you:
myStr.replaceAll(Pattern.quote("+"), replaceStr);

- 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
-
1Why would the replaceStr have to be quoted? String.replace() doesn't replace all occurrences. – FelixM May 17 '12 at 15:38
-
1Pretty 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
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);

- 14,426
- 7
- 55
- 65
Others have already stated the correct method of:
- Escaping the
+
as\\+
- 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);

- 445,704
- 82
- 492
- 529
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");

- 42,159
- 20
- 102
- 127
-
thx for pointing to this. helped me remembering using this non-regex solution in simple cases. – Gerhard Dinhof Jan 14 '10 at 08:00
-
2isn't that equivalent to using mystr.replace("+", "plus") ? replace does not use regex (while replaceAll does). – Vinze Jun 08 '10 at 10:16
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);
-
2Shouldn't the second example be `Pattern.quote("+")` and not `Pattern.quote(str)`? – Adam Jensen Aug 08 '14 at 05:25
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.

- 1