简体   繁体   English

替换字符串az 0-9和旁边的所有字符,

[英]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.

相关问题 拒绝包含az,AZ,0-9以外字符的字符串 - Reject a string that contains characters other than a-z, A-Z, 0-9 正则表达式替换所有前导字符而不是a-Z - Regex to replace all leading characters not a-Z Java Regex az,AZ,0-9和(。)(_)( - ) - Java Regex a-z, A-Z, 0-9 and (.)(_)(-) 如何仅使用Java从文本文件中读取字母(az || AZ)和数字(0-9)字符? - How to read alphabets (a-z || A-Z) and digits (0-9) characters from a text file only in Java? 生成由az和0-9组成的1-5个字符长的字符串 - Generate one to five character length string consisting of a-z and 0-9 包含除 [AZ][az][0-9][\\s][-] 以外的任何字符串的正则表达式 - Regex for string containing anything other than [A-Z][a-z][0-9][\s][-] Java:如何删除字符串中除az,数字和德语字符以外的所有字符 - Java : How to remove all characters in String except a a-z,digits and German characters REGEX az 0-9但不仅仅是数字 - REGEX a-z 0-9 but not only numbers 如何将拉丁语unicode字符替换为[az]字符 - how to replace Latin unicode character to [a-z] characters 如何制作一个正则表达式来匹配以 0-9 或 az 开头的带有重音符号的字符串,并且必须只接受这个特殊字符 - 单词之间的 _ '? - How to make a regex to match string that starts with 0-9 or a-z with accents and must accept only this special character - _ ' between words?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM