![](/img/trans.png)
[英]Remove non numeric characters from string and cast numbers as an int into an array
[英]Remove numbers and characters only from string
給定一個包含數字和字符的字符串,例如
Mi2ch£al
什么是相關的正則表達式來去除一切,但字母(因此數字和字符)?
另外,我正在使用.NET 2.0進行此任務。
string s = "Mi2ch£al";
s = Regex.Replace(s, @"[^\w\s]", "");
如果您不想使用國際重音符號:
string s = "Mi2ch£al";
s = Regex.Replace(s, @"[^a-zA-Z0-9\s]", "");
用空字符串或[^a-zA-Z\\s]
替換正則表達式[^a-zA-Z]
[^a-zA-Z\\s]
可以節省空間
如果您不介意使用Linq:
string s = new string("Mi2ch£al".Where(c => !char.IsNumber(c) && !char.IsLetter(c)).ToArray());
\\p{L}
是一個字母匹配任何字符,和\\P{L}
這不是一個字母(包括非拉丁字符集,重音符號等)任何字符。 因此,您可以簡單地使用:
Regex.Replace(input, @"\P{L}", String.Empty)
其中input
是輸入字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.