[英]Replace all characters in string beside a-z 0-9 and ,
嘿,我想清理一个字符串,只允许它具有az AZ(还有其他语言,不仅是英语),而且,我尝试执行ReplaceAll([^az 0-9,])
但它正在删除其他语言。有人告诉我如何才能仅对特殊字符进行消毒,并且也不会从中删除表情符号?
You could try getting the az and 0-9 characters' ASCII code, and if the current character is not one of them, do what you wish. 您可以尝试获取az和0-9字符的ASCII码,如果当前字符不是其中之一,请执行所需的操作。 On how to get the ascii value of a character, refer here . 有关如何获取字符的ascii值,请参见此处 。
EDIT: the idea is that az and 0-9 the characters are next to each other. 编辑:想法是az和0-9字符彼此相邻。 So just write a simple function that returns a boolean
whether your current character is one of these, and if not, replace. 因此,只需编写一个简单的函数即可返回一个boolean
无论您当前的字符是否是其中之一,否则返回。 For this though, you will have to replace one by one. 为此,您将必须一一替换。
I've tested this regular expression and AFAIK it works... 我已经测试了此正则表达式和AFAIK的工作原理...
String result = yourString.replaceAll("[^a-zA-Z0-9]", "");
It replaces any character that isn't in the set az, AZ, or 0-9 with nothing. 它会用任何字符替换掉不在az,AZ或0-9中的任何字符。
In java you can do 在java中你可以做
yourString.replaceAll("[^\\p{L}\\p{Nd}]+", "");
The regular expression [^\\p{L}\\p{Nd}]+
match all characters that are no a unicode letter or a decimal number. 正则表达式[^\\p{L}\\p{Nd}]+
匹配所有不是unicode字母或十进制数字的字符。
If you need only characters (not numbers) you can use the regular expression [^\\\\p{L}]+
as follow: 如果只需要字符(而不是数字),则可以使用正则表达式[^\\\\p{L}]+
,如下所示:
yourString.replaceAll("[^\\p{L}]+", "");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.