字符串替换使用$ sign抛出错误

时间:2022-03-01 04:17:52

I'm having an issue with replacing a string in java...

我在替换java中的字符串时遇到问题...

the line is:

这条线是:

subject = subject.replaceAll("\\[calEvent\\]", calSubject);

This line doesn’t work with $ sign in calSubject.

此行不适用于calSubject中的$ sign。

what the subject variable is, a dynamic subject line variable from a file. for example like so: Calnot = [calEvent]

主题变量是什么,来自文件的动态主题行变量。比如像:Calnot = [calEvent]

what i am trying to do is replace the calEvent place holder with the subject variable. but how i did it does not work because it crashes when the subject contains a $ sign.

我想要做的是用主题变量替换calEvent占位符。但我怎么做它不起作用,因为它在主题包含$符号时崩溃。

any idea how I can do this so it won't break if the subject contains a $ sign or any characters for that matter?

任何想法如何做到这一点,如果主题包含$符号或任何字符的话,它不会破坏?

5 个解决方案

#1


21  

That's because the dollar sign is a special character in a replacement string, use Matcher.quoteReplacement() to escape this kind of character.

那是因为美元符号是替换字符串中的特殊字符,使用Matcher.quoteReplacement()来转义这种字符。

subject = subject.replaceAll("\\[calEvent\\]", Matcher.quoteReplacement(calSubject));

From the doc of String.replaceAll() :

从String.replaceAll()的doc:

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; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果与将其视为文字替换字符串时的结果不同;见Matcher.replaceAll。如果需要,使用Matcher.quoteReplacement(java.lang.String)来抑制这些字符的特殊含义。

Note that the dollar sign is used to refer to the corresponding capturing groups in the regular expression ($0, $1, etc.).

请注意,美元符号用于指代正则表达式中的相应捕获组($ 0,$ 1等)。

EDIT

编辑

Matcher.quoteReplacement() has been introduced in Java 1.5, if you're stuck in Java 1.4 you have to escape $ manually by replacing it with \$ inside the string. But since String.replaceAll() would also take the \ and the $ as special characters you have to escape them once and you also have to escape all \ once more for the Java runtime.

Matcher.quoteReplacement()已经在Java 1.5中引入了,如果你陷入Java 1.4,你必须通过在字符串中用\ $替换它来手动转义$。但是因为String.replaceAll()也会将\和$作为特殊字符,所以你必须将它们转义一次,你还必须再次为Java运行时转义\。

("$", "\$") /* what we want */
("\$", "\\\$") /* RegExp engine escape */
("\\$", "\\\\\\$") /* Java runtime escape */

So we get :

所以我们得到:

calSubject = calSubject.replaceAll("\\$", "\\\\\\$");  

#2


5  

if you don't need the regex feature, you can consider to use this method of String class: replace(CharSequence target,CharSequence replacement)

如果你不需要正则表达式功能,你可以考虑使用String类的这个方法:replace(CharSequence target,CharSequence replacement)

It saves your "escape" backslashes as well.

它也可以保存你的“逃逸”反斜杠。

api doc:

api doc:

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

将此字符串中与文字目标序列匹配的每个子字符串替换为指定的文字替换序列。替换从字符串的开头到结尾,例如,在字符串“aaa”中将“aa”替换为“b”将导致“ba”而不是“ab”。

#3


2  

From the documentation of replaceAll:

从replaceAll的文档:

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; see Matcher.replaceAll. Use java.util.regex.Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.

请注意,替换字符串中的反斜杠()和美元符号($)可能导致结果与将其视为文字替换字符串时的结果不同;见Matcher.replaceAll。如果需要,请使用java.util.regex.Matcher.quoteReplacement来抑制这些字符的特殊含义。

And in Matcher.replaceAll

在Matcher.replaceAll

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.

如上所述,美元符号可被视为对捕获的子序列的引用,反斜杠用于替换替换字符串中的文字字符。

#4


0  

Not sure I really understand your question but try

不确定我真的理解你的问题,但试试

subject = subject.replaceAll("\\[calEvent\\]", Matcher.quoteReplacement(calSubject));

#5


0  

Please use

请用

Matcher.quoteReplacement(calEvent);

#1


21  

That's because the dollar sign is a special character in a replacement string, use Matcher.quoteReplacement() to escape this kind of character.

那是因为美元符号是替换字符串中的特殊字符,使用Matcher.quoteReplacement()来转义这种字符。

subject = subject.replaceAll("\\[calEvent\\]", Matcher.quoteReplacement(calSubject));

From the doc of String.replaceAll() :

从String.replaceAll()的doc:

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; see Matcher.replaceAll. Use Matcher.quoteReplacement(java.lang.String) to suppress the special meaning of these characters, if desired.

请注意,替换字符串中的反斜杠(\)和美元符号($)可能会导致结果与将其视为文字替换字符串时的结果不同;见Matcher.replaceAll。如果需要,使用Matcher.quoteReplacement(java.lang.String)来抑制这些字符的特殊含义。

Note that the dollar sign is used to refer to the corresponding capturing groups in the regular expression ($0, $1, etc.).

请注意,美元符号用于指代正则表达式中的相应捕获组($ 0,$ 1等)。

EDIT

编辑

Matcher.quoteReplacement() has been introduced in Java 1.5, if you're stuck in Java 1.4 you have to escape $ manually by replacing it with \$ inside the string. But since String.replaceAll() would also take the \ and the $ as special characters you have to escape them once and you also have to escape all \ once more for the Java runtime.

Matcher.quoteReplacement()已经在Java 1.5中引入了,如果你陷入Java 1.4,你必须通过在字符串中用\ $替换它来手动转义$。但是因为String.replaceAll()也会将\和$作为特殊字符,所以你必须将它们转义一次,你还必须再次为Java运行时转义\。

("$", "\$") /* what we want */
("\$", "\\\$") /* RegExp engine escape */
("\\$", "\\\\\\$") /* Java runtime escape */

So we get :

所以我们得到:

calSubject = calSubject.replaceAll("\\$", "\\\\\\$");  

#2


5  

if you don't need the regex feature, you can consider to use this method of String class: replace(CharSequence target,CharSequence replacement)

如果你不需要正则表达式功能,你可以考虑使用String类的这个方法:replace(CharSequence target,CharSequence replacement)

It saves your "escape" backslashes as well.

它也可以保存你的“逃逸”反斜杠。

api doc:

api doc:

Replaces each substring of this string that matches the literal target sequence with the specified literal replacement sequence. The replacement proceeds from the beginning of the string to the end, for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

将此字符串中与文字目标序列匹配的每个子字符串替换为指定的文字替换序列。替换从字符串的开头到结尾,例如,在字符串“aaa”中将“aa”替换为“b”将导致“ba”而不是“ab”。

#3


2  

From the documentation of replaceAll:

从replaceAll的文档:

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; see Matcher.replaceAll. Use java.util.regex.Matcher.quoteReplacement to suppress the special meaning of these characters, if desired.

请注意,替换字符串中的反斜杠()和美元符号($)可能导致结果与将其视为文字替换字符串时的结果不同;见Matcher.replaceAll。如果需要,请使用java.util.regex.Matcher.quoteReplacement来抑制这些字符的特殊含义。

And in Matcher.replaceAll

在Matcher.replaceAll

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.

如上所述,美元符号可被视为对捕获的子序列的引用,反斜杠用于替换替换字符串中的文字字符。

#4


0  

Not sure I really understand your question but try

不确定我真的理解你的问题,但试试

subject = subject.replaceAll("\\[calEvent\\]", Matcher.quoteReplacement(calSubject));

#5


0  

Please use

请用

Matcher.quoteReplacement(calEvent);