用于替换除下划线以外的所有特殊字符的 Java 正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13494912/
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
Java regex for replacing all special characters except underscore
提问by meyerjp3
I would like a regular expression for removing all special characters except an underscore. I can replace all special characters, but I don't know how to keep the underscores. Here's the code for removing all speial characters,
我想要一个正则表达式来删除除下划线之外的所有特殊字符。我可以替换所有特殊字符,但我不知道如何保留下划线。这是删除所有特殊字符的代码,
String myname= "!john_smith@#-".replaceAll("\p{Punct}+", "");
Any ideas how to modify the regex so that I keep underscores?
任何想法如何修改正则表达式以便我保留下划线?
Thanks, Patrick
谢谢,帕特里克
回答by Martin Ender
Well \\p...
is the same as [^\\P...]
(negated character class of characters nothaving the property), and then you could put the underscore in there as well:
Well\\p...
与[^\\P...]
(不具有该属性的字符的否定字符类)相同,然后您也可以将下划线放在那里:
"[^\P{Punct}_]+"
Alternatively, use a negative lookahead
或者,使用负前瞻
"(?:(?!_)\p{Punct})+"
Also, seeing your example, maybe something as simple as this will be enough for you:
另外,看到你的例子,也许像这样简单的事情对你来说就足够了:
"[^\w\s]+"
Will remove everything except for letters, digits, underscores and whitespace.
将删除除字母、数字、下划线和空格之外的所有内容。
回答by Marko Topolnik
Use set difference:
使用集差:
System.out.println("#%a#%^$^_#$%b#$".replaceAll("[\p{Punct}&&[^_]]", ""));
prints
印刷
a_b
Reference: Character Classes
参考:字符类