简体   繁体   English

如何在不删除@符号的情况下从字符串中删除所有非字母数字字符

[英]How to remove all non alphanumeric chars from a String without remove @ symbols

currently using this regex: s.replaceAll("[^a-zA-Z0-9]", ""); 当前使用此正则表达式: s.replaceAll("[^a-zA-Z0-9]", ""); however it removes values I need such as @ . , ! 但是它将删除我需要的值,例如@ . , ! @ . , ! how can I include these in my regex so they do not get replaced with empty strings. 我如何在我的正则表达式中包括这些,以免它们被空字符串替换。

Cheers 干杯

Just add them to the regex. 只需将它们添加到正则表达式即可。 Note that . 注意. is a special character, so you'd have to escape it: 是一个特殊字符,因此您必须对其进行转义:

String newString = s.replaceAll("[^a-zA-Z0-9!@\\.,]", "");

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

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