Java:将字符串中的所有 ' 替换为 \'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20556101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Replace all ' in a string with \'
提问by Iwan Eising
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.
我试过使用replaceAll,但它没有做任何事情。出于某种原因,我无法让正则表达式工作。
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
我要
回答by Sage
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:
您必须首先转义反斜杠,因为它是文字 (yielding \\
),然后由于正则表达式 (yielding \\\\
)再次转义它。所以,尝试:
s.replaceAll("'", "\\'");
output:
输出:
You\'ll be totally awesome, I\'m really terrible
回答by kosa
回答by LeoPleurodon
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
您也可以尝试使用 StringEscapeUtils 之类的东西让您的生活更轻松:http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html
s = StringEscapeUtils.escapeJava(s);
回答by user2864740
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:
使用String.replaceAll
或任何适用的 Matcher 替换器时,请注意替换字符串及其处理方式:
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:
正如 isnot2bad 在评论中指出的那样,Matcher.quoteReplacement
这里可能有用:
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.
返回指定字符串的文字替换字符串。.. 生成的字符串将匹配 s 中被视为文字序列的字符序列。斜线 (
\
) 和美元符号 ($
) 没有特殊含义。
回答by Pshemo
Let's take a tour of String#repalceAll(String regex, String replacement)
让我们来看看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)
以 str.replaceAll(regex, repl) 形式调用此方法会产生与表达式完全相同的结果
Pattern.compile(regex).matcher(str).replaceAll(repl)
So lets take a look at Matcher.html#replaceAll(java.lang.String)documentation
所以让我们看看Matcher.html#replaceAll(java.lang.String)文档
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 subsequencesas 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
你可以看到,在replacement
我们有特殊字符$
可以用作被捕获组的参考,如
System.out.println("aHellob,aWorldb".replaceAll("a(\w+?)b", ""));
// 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 "\\\\"
.
因此,如果您想创建\
替换零件,则需要\
在它之前添加另一个零件。但是请记住,要\
在 String 中编写文字,您需要将其编写为"\\"
创建两个\\
替换,您需要将其编写为"\\\\"
.
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
减少更换部件明确的逃逸(也包括正则表达式的一部分-忘了提到,早期的),只要用replace
代替replaceAll
它增加了正则表达式逃逸我们
s = s.replace("'", "\'");
回答by Bogdan Kobylynskyi
You can use apache's commons-textlibrary (instead of commons-lang):
您可以使用 apache 的commons-text库(而不是 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>