简体   繁体   English

Java replaceAll失败,源字符串中有美元符号

[英]Java replaceAll fails with dollar sign in source string

Say I have the following code 说我有以下代码

String test = "$abc<>";
test = test.replaceAll("[^A-Za-z0-9./,#-' ]", "");

test is now "$abc". 测试现在是“$ abc”。

Why does it keep the dollar sign? 为什么它保持美元符号?

Your list of characters to preserve includes #-' , which is a range from Unicode U+0023 (the # symbol) to U+0027 (the ' symbol), including $ (U+0024). 要保留的字符列表包括#-' ,它是从Unicode U + 0023( #符号)到U + 0027( '符号)的范围,包括$ (U + 0024)。

If you meant #-' to be interpreted as a list of three characters, just escape it: 如果你的意思是#-'被解释为三个字符的列表,那就逃避吧:

test = test.replaceAll("[^A-Za-z0-9./,#\\-' ]", "");

or put it at the end of the list: 或者把它放在列表的末尾:

test = test.replaceAll("[^A-Za-z0-9./,#' -]", "");

Because you must put the - as the last character in your character class. 因为你必须把-作为你角色类中的最后一个角色。

Try 尝试

test.replaceAll("[^A-Za-z0-9./,#' -]", "");

It'll work :) 它会工作:)

See also In a java regex, how can I get a character class eg [az] to match a - minus sign? 另请参见在java正则表达式中,如何获取字符类,例如[az]以匹配 - 减号?

and the Javadoc for Pattern (Ctrl-F "Character classes") 模式的Javadoc(Ctrl-F“字符类”)

Note that a different set of metacharacters are in effect inside a character class than outside a character class. 请注意,字符类中的一组不同的元字符在字符类外部有效。 For instance, the regular expression . 例如,正则表达式。 loses its special meaning inside a character class, while the expression - becomes a range forming metacharacter. 在字符类中失去其特殊含义,而表达式 - 成为形成元字符的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM