Java RegEx:匹配任何非单词和非数字字符,除了
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18244482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RegEx: match any non-word and non-digit character except
提问by TomatoMato
To match any non-word and non-digit character (special characters) I use this: [\\W\\D]
. What should I add if I want to also ignore some concrete characters? Let's say, underscore.
为了匹配任何非单词和非数字字符(特殊字符),我使用这个:[\\W\\D]
. 如果我还想忽略一些具体字符,我应该添加什么?比方说,下划线。
采纳答案by Rohit Jain
First of all, you must know that \W
is equivalent to [^a-zA-Z0-9_]
. So, you can change your current regex to:
首先,你必须知道这\W
相当于[^a-zA-Z0-9_]
. 因此,您可以将当前的正则表达式更改为:
[\W]
This will automatically take care of \D
.
这将自动处理\D
。
Now, if you want to ignore some other character, say &
(underscore is already exluded in \W
), you can use negated character class:
现在,如果您想忽略其他一些字符,例如&
(下划线已被排除在 中\W
),您可以使用否定字符类:
[^\w&]