2014-12-02 80 views

回答

6

添加\p{L}到否定字符类,而不是a-zA-Z\p{L}匹配来自任何语言的任何种类的信件。通过将这个添加到否定字符类将匹配任何字符,但不是字母。

@"[^\p{L}0-9 -]" 

DEMO

string str = "abc // t?% ?? ttt ,. y Ä Ö Ü ä, ö !"; 
string result = Regex.Replace(str, @"[^\p{L}0-9 -]", ""); 
Console.WriteLine(result); 
Console.ReadLine(); 

输出:

abc t ttt y Ä Ö Ü ä ö 

IDEONE

+1

可以说,与所有字母使用Unicode统一''L相同的基础上,所有数字都应该使用'N':'[^ \ p {L} \ p {N} - ]'。 – Richard 2014-12-02 14:08:56

1
Func<char, bool> filter = ch => char.IsLetterOrDigit(ch) || 
           char.IsWhiteSpace(ch) || 
           ch == '-'; 

var abc = new string(str.Where(filter).ToArray()); 

小提琴:https://dotnetfiddle.net/MBRsPX

+1

更新:这将删除空间和' - '您的示例在dotnetfiddle删除空间,' - '和数字 – fubo 2014-12-02 14:10:42

+0

@fubo再次感谢,修正 – dcastro 2014-12-02 14:16:30

+0

+我喜欢那个委托方法 – fubo 2014-12-02 14:19:00

相关问题