Java 如何用\替换字符串中的一个或多个\?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23869613/
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
How to replace one or more \ in string with just \?
提问by Destructor
Consider the string,
考虑字符串,
this\is\a\new\string
The output should be:
输出应该是:
this\is\a\new\string
So basically one or more \
character should be replaced with just one \
.
I tried the following:
所以基本上一个或多个\
字符应该只替换为一个\
. 我尝试了以下方法:
str = str.replace("[\]+","\")
but it was no use. The reason I used two \
in [\\]+
was because internally \
is stored as \\
. I know this might be a basic regex question, but I am able to replace one or more normal alphabets but not \
character. Any help is really appreciated.
但没有用。我使用两个\
in的原因[\\]+
是因为内部\
存储为\\
. 我知道这可能是一个基本的正则表达式问题,但我可以替换一个或多个普通字母而不是\
字符。任何帮助都非常感谢。
采纳答案by Pshemo
str.replace("[\\]+", "\")
has few problems,
str.replace("[\\]+", "\")
问题很少,
replace
doesn't use regex (replaceAll
does) so"[\\]"
will represent[\]
literal, not\
nor\\
(depending on what you think it would represent)- even if it did accept regex
"[\\]"
would not be correct regex because\\]
would escape]
so you would end up with unclosed character class[..
- it will not compile (your replacement String is not closed)
replace
不使用正则表达式(replaceAll
确实)所以"[\\]"
将代表[\]
文字,而\
不是\\
(取决于你认为它代表什么)- 即使它确实接受正则表达式
"[\\]"
也不是正确的正则表达式,因为\\]
会转义,]
因此您最终会得到未关闭的字符类[..
- 它不会编译(您的替换字符串未关闭)
It will not compile because \
is start of escape sequence \X
where X
needs to be either
它不会编译,因为它\
是转义序列的开始\X
,X
需要是
- changed from being String special character to simple literal, like in your case
\"
will escape"
to be literal (so you could print it for instance) instead of being start/end of String, - changed from being normal character to be special one like in case of line separators
\n
\r
or tabulations\t
.
- 从字符串特殊字符更改为简单文字,就像在您的情况下
\"
将转义"
为文字(例如,您可以打印它)而不是字符串的开始/结束, - 从正常字符变为特殊字符,例如行分隔符
\n
\r
或制表符\t
。
Now we know that \
is special and is used to escape other character. So what do you think we need to do to make \
represent literal (when we want to print \
). If you guessed that it needs to be escaped with another \
then you are right. To create \
literal we need to write it in String as "\\"
.
现在我们知道这\
是特殊的,用于转义其他字符。那么你认为我们需要做什么来使\
代表文字(当我们想要打印时\
)。如果您猜测它需要与另一个转义,\
那么您是对的。要创建\
文字,我们需要将它写在 String as 中"\\"
。
Since you know how to create String containing \
literal (escaped \
) you can start thinking about how to create your replacements.
既然您知道如何创建包含\
文字(转义\
)的字符串,您就可以开始考虑如何创建替换。
Regex which represents one or more \
can look like
代表一个或多个的正\
则表达式看起来像
\\+
\\+
But that is its native form, and we need to create it using String. I used \\
here because in regex \
is also special character (for instance \d
represents digits, not \
literal followed by d
) so it also needs to be escaped first to represent \
literal. Just like in String we can escape it with another \
.
但这是它的原生形式,我们需要使用 String 来创建它。我\\
在这里使用\
是因为在正则表达式中也是特殊字符(例如\d
表示数字,而不是\
文字后跟d
)所以它也需要先转义以表示\
文字。就像在 String 中一样,我们可以用另一个\
.
So String representing this regex will need to be written as
所以表示这个正则表达式的字符串需要写成
"\\\\+"
(we escaped \
twice, once in regex \\+
and once in string)
"\\\\+"
(我们转义了\
两次,一次在正则表达式中\\+
,一次在字符串中)
You can use it as first argument of replaceAll
(because replace
as mentioned earlier doesn't accept regex).
您可以将其用作replaceAll
( 因为replace
如前所述不接受正则表达式) 的第一个参数。
Now last problem you will face is second argument of replaceAll
method. If you write
现在您将面临的最后一个问题是replaceAll
方法的第二个参数。如果你写
replaceAll("\\\\+", "\\")
replaceAll("\\\\+", "\\")
and it will find match for regex you will see exception
它会找到正则表达式的匹配,你会看到异常
java.lang.IllegalArgumentException: character to be escaped is missing
It is because in replacement
part (second argument in replaceAll
method) we can also use special formula $x
which represents current match from group with index x
. So to be able to escape $
into literal we need some escape mechanism, and again \
was used here for that purpose. So \
is also special in replacement partof our method.
So again to create \
literal we need to escape it with another \
, and string literal representing expression \\
is "\\\\"
.
这是因为replacement
部分(replaceAll
方法中的第二个参数)我们还可以使用特殊公式$x
来表示来自具有 index 的组的当前匹配x
。因此,为了能够转义$
为字面意思,我们需要一些转义机制,\
此处再次用于此目的。所以\
我们方法的替换部分也很特别。
所以再次创建\
文字我们需要用另一个转义它\
,并且字符串文字表示表达式\\
is "\\\\"
。
But lets get back to earlier exception: message "character to be escaped is missing" refers to X
part of \X
formula (X is character we want to be escaped). Problem is that earlier your replacement "\\"
represented only \
part, so this method expected either $
to create \$
, or \\
to create \
literal. So valid replacements would be "\\$
or "\\\\"
.
但是让我们回到之前的异常:消息“要转义的字符丢失”指的X
是\X
公式的一部分(X 是我们想要转义的字符)。问题是之前你的替换"\\"
只代表了\
一部分,所以这个方法期望要么$
创建\$
,要么\\
创建\
文字。所以有效的替换是"\\$
or "\\\\"
。
To make things work you need to write your replacing method as
为了使事情工作,您需要将替换方法编写为
str = str.replaceAll("\\+", "\\")
回答by anubhava
You can use:
您可以使用:
str = str.replace("\\", "\");
Remember that String#replace
doesn't take a regex.
请记住,这String#replace
不需要正则表达式。
回答by JLRishe
When writing regular expressions, you typically need to double-escape backslashes. So you would do this:
编写正则表达式时,您通常需要对反斜杠进行双重转义。所以你会这样做:
str = str.replaceAll("\\+", "\\");
回答by Evgeniy Dorofeev
try this
尝试这个
str = str.replaceAll("\\+", "\\");
回答by Micha F.
I'd use Matcher.quoteReplacement()
and String.replaceAll()
here.
我会在这里使用Matcher.quoteReplacement()
和String.replaceAll()
。
Like this:
像这样:
String s;
[...]
s = s.replaceAll("\\+", Matcher.quoteReplacement("\"));