2014-12-02 79 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

相關問題