如何删除所有的除外串字母数字字符 [英] How to remove all except alphanumeric characters from string
本文介绍了如何删除所有的除外串字母数字字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从以下字符串中删除所有特殊字符:
ABC // T%? TTT。 Y AÖU A,哦!
使用正则表达式:
正则表达式RGX =新的正则表达式([^ A-ZA-Z0-9 - ]);
但这个表达式还可以删除邻U A,ö
,但我想保持这些字符。
我想删除只喜欢字符: @#$%^&安培;,;:'......
< DIV CLASS =h2_lin>解决方案
添加 \p {L}
进入否定的字符类,而不是 AZ
, AZ
。 \p {L}
匹配任何语言的任何信件。通过添加这一个否定的字符类将匹配任何字符但不是字母。
@[^ \p {L } 0-9 - ]
的演示
字符串str =ABC // T'%?? TTT。 Y AöU A,ö!;
字符串结果= Regex.Replace(STR,@[^ \p {L} 0-9 - ],);
Console.WriteLine(结果);
到Console.ReadLine();
输出:
ABC牛逼TTT Y A O u那样邻
I am trying to remove all special characters from the following string:
abc // t?% ?? ttt ,. y Ä Ö Ü ä, ö !
With regex:
Regex rgx = new Regex("[^a-zA-Z0-9 -]");
But this regex also remove Ä Ö Ü ä, ö
but I want to keep those characters.
I want to remove only characters like: !@#$%^&,;:'....
解决方案
Add \p{L}
into the negated character class instead of a-z
, A-Z
. \p{L}
matches any kind of letter from any language. By adding this to a negated character class would match any character but not of letters.
@"[^\p{L}0-9 -]"
string str = "abc // t?% ?? ttt ,. y Ä Ö Ü ä, ö !";
string result = Regex.Replace(str, @"[^\p{L}0-9 -]", "");
Console.WriteLine(result);
Console.ReadLine();
Output:
abc t ttt y Ä Ö Ü ä ö
这篇关于如何删除所有的除外串字母数字字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文