[英]Why does replaceAll remove the numbers from the String?
String foo = "a3#4#b";
String afterPunctutationRemoval = foo.replaceAll("[,.;:?!'-_\"/()\\{}]", "");
System.out.println(afterPunctutationRemoval);
it gives me "a##b" as a result, can someone explain me why? 它给了我一个“## b”的结果,有人可以解释我为什么吗?
Shouldn't it return the string as it is? 它不应该按原样返回字符串吗?
Your character class contains the range '
.. _
which matches digits as well. 您的角色类包含范围'
.. _
,它也匹配数字。
Put the -
at the start or end of the character class: 把-
放在字符类的开头或结尾:
foo.replaceAll("[,.;:?!'_\"/()\\{}-]", "")
or escape it: 或逃避它:
foo.replaceAll("[,.;:?!'\\-_\"/()\\{}]", "");
'-_
匹配'
和_
之间'
每个字符。
Like the other two people said, escape the -
character - \\\\-
. 就像其他两个人说的那样,逃避-
角色 - \\\\-
。
Also, \\\\{
evaluates to the same as {
, was this intended? 另外, \\\\{
评价与{
相同,是这个意图吗? If not, try escaping the backslash - \\\\\\\\{
如果没有,请尝试转义反斜杠 - \\\\\\\\{
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.