<分区>
我似乎无法将一串“:)”替换为其他内容,这是我的代码:
if(message.contains(":)")) message = message.replaceAll(":)", replacement);
这是错误:
Exception in thread "Listen" java.util.regex.PatternSyntaxException: Unmatched closing ')'
near index 0
:)
^
我该怎么办?
<分区>
我似乎无法将一串“:)”替换为其他内容,这是我的代码:
if(message.contains(":)")) message = message.replaceAll(":)", replacement);
这是错误:
Exception in thread "Listen" java.util.regex.PatternSyntaxException: Unmatched closing ')'
near index 0
:)
^
我该怎么办?
最佳答案
不要使用replaceAll()
;使用 replace()
当你想替换文字字符串时:
message.replace(":)", replacement)
replaceAll()
处理 regular expressions ,其中 )
具有特殊含义,因此会出现错误。
关于java - 为什么我不能替换 ":)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21076074/