![](/img/trans.png)
[英]How to remove all special Characters from a string except - . and space
[英]Remove all special characters from a string except for digits and the degree symbol?
我想从字符串中删除所有特殊字符,除了数字和度数符号°。 如何使用正则表达式执行此操作?
我知道.replaceAll(“ [^ a-zA-Z0-9] +”,“”)会删除所有特殊字符,如何将度数符号放回去?
您可以使用\\W
并使用字符类减法 :
.replaceAll("[\\W_&&[^°]]+","")
另一种方法是将度数符号添加到您拥有的否定字符类中,因为否定字符类会匹配除字符类中的字符以外的所有其他字符:
.replaceAll("[^a-zA-Z0-9°]+","")
^
经过测试: http : //www.ocpsoft.org/tutorials/regular-expressions/java-visual-regex-tester/ 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.