Java: Replace all ' in a string with \'
Asked Answered
C

6

24

I need to escape all quotes (') in a string, so it becomes \'

I've tried using replaceAll, but it doesn't do anything. For some reason I can't get the regex to work.

I'm trying with

String s = "You'll be totally awesome, I'm really terrible";
String shouldBecome = "You\'ll be totally awesome, I\'m really terrible";
s = s.replaceAll("'","\\'"); // Doesn't do anything
s = s.replaceAll("\'","\\'"); // Doesn't do anything
s = s.replaceAll("\\'","\\'"); // Doesn't do anything

I'm really stuck here, hope somebody can help me here.

Thanks,

Iwan

Cyton answered 12/12, 2013 at 23:5 Comment(2)
possible duplicate of replace() and replaceAll() in JavaBorodino
In case you haven't considered this yet, if the input is from a user in any way, you might also want to replace any backslashes with double backslashes first. Like if the user enters "You are 'awesome'\'amazing'", then you currently would get "You are \'awesome\'\\'amazing\'". That leaves the 3rd quote unescaped because that user-entered backslash is escaping the generated backslack after it!Triangulation
L
31

You have to first escape the backslash because it's a literal (yielding \\), and then escape it again because of the regular expression (yielding \\\\). So, Try:

 s.replaceAll("'", "\\\\'");

output:

You\'ll be totally awesome, I\'m really terrible
Ling answered 12/12, 2013 at 23:7 Comment(8)
Matcher.quoteReplacement("\\'") can be used to quote the replacement string.Concertize
@Concertize I really think the usage of Matcher.quoteReplacement almost deserves an answer itself. (It is quoteReplacement, not quoteRegex for reason.)Borodino
@user2864740, could you please elaborate a little. I have explained it in terms of replace function character sequence as for this case we would not need four backslash but only two as Nambari answered.Ling
My complaint is with ".. because of the regular expression ..", which is wrong. It is a replacement string, not a regular expression.Borodino
@user2864740, yes but what i meant is that to be replaced with this regular expression of replaceAll we will need to re-escape itLing
The regular expression is denoted by the string literal "'". The method isn't a regular expression, but it uses a regular expression.Borodino
Can you please tell me what to do if I have to replace " with \" ?Bangle
this is not working please suggest some other methodRedeploy
C
12

Use replace()

 s = s.replace("'", "\\'"); 

output:

You\'ll be totally awesome, I\'m really terrible

Confusion answered 12/12, 2013 at 23:8 Comment(2)
@BoristheSpider: replaceall() is different from replace(). I don't think OP need regex here.Confusion
this is not working, please suggest some other methodRedeploy
N
10

Let's take a tour of String#repalceAll(String regex, String replacement)

You will see that:

An invocation of this method of the form str.replaceAll(regex, repl) yields exactly the same result as the expression

Pattern.compile(regex).matcher(str).replaceAll(repl)

So lets take a look at Matcher.html#replaceAll(java.lang.String) documentation

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

You can see that in replacement we have special character $ which can be used as reference to captured group like

System.out.println("aHellob,aWorldb".replaceAll("a(\\w+?)b", "$1"));
// result Hello,World

But sometimes we don't want $ to be such special because we want to use it as simple dollar character, so we need a way to escape it.
And here comes \, because since it is used to escape metacharacters in regex, Strings and probably in other places it is good convention to use it here to escape $.

So now \ is also metacharacter in replacing part, so if you want to make it simple \ literal in replacement you need to escape it somehow. And guess what? You escape it the same way as you escape it in regex or String. You just need to place another \ before one you escaping.

So if you want to create \ in replacement part you need to add another \ before it. But remember that to write \ literal in String you need to write it as "\\" so to create two \\ in replacement you need to write it as "\\\\".


So try

s = s.replaceAll("'", "\\\\'");

Or even better

to reduce explicit escaping in replacement part (and also in regex part - forgot to mentioned that earlier) just use replace instead replaceAll which adds regex escaping for us

s = s.replace("'", "\\'");
Nibble answered 13/12, 2013 at 0:55 Comment(0)
B
4

This doesn't say how to "fix" the problem - that's already been done in other answers; it exists to draw out the details and applicable documentation references.


When using String.replaceAll or any of the applicable Matcher replacers, pay attention to the replacement string and how it is handled:

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

As pointed out by isnot2bad in a comment, Matcher.quoteReplacement may be useful here:

Returns a literal replacement String for the specified String. .. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes (\) and dollar signs ($) will be given no special meaning.

Borodino answered 13/12, 2013 at 0:47 Comment(0)
S
1

You could also try using something like StringEscapeUtils to make your life even easier: http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html

s = StringEscapeUtils.escapeJava(s);
Sorrells answered 12/12, 2013 at 23:9 Comment(0)
O
0

You can use apache's commons-text library (instead of commons-lang):

Example code:

org.apache.commons.text.StringEscapeUtils.escapeJava(escapedString);

Dependency:

compile 'org.apache.commons:commons-text:1.8'

OR

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-text</artifactId>
   <version>1.8</version>
</dependency>
Owens answered 8/4, 2020 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.