本文介绍了替换除一个之外的所有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
string = str.replaceAll("\\W", " ")
用(空格)替换所有特殊字符。
This replace all special character by " " (space).
但我尝试将短划线 - 排除为特殊字符。
But I try to exclude dash "-" as special character.
这是我的尝试:
string = str.replaceAll("\\W[^-]", " ")
但这不是我所期待的。
Q :我该如何使用?
推荐答案
如果你想匹配除 \w
和 -
以外的所有字符,你可以使用:
If you want to match all the characters except \w
and -
you can use:
[^\w-]
例如:
str.replaceAll("[^\\w-]+", " ")
这篇关于替换除一个之外的所有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!