Given a string containing numbers and characters eg
Mi2ch£al
What would be the relevant regex to remove everything but letters (therefore numbers and characters)?
Also, I am using .NET 2.0 for this task.
string s = "Mi2ch£al";
s = Regex.Replace(s, @"[^\w\s]", "");
and if you don't want international accented characters:
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}
matches any character that is a letter, and \\P{L}
any character that is not a letter (including non-Latin character sets, accented characters, etc.). Thus, you could simply use:
Regex.Replace(input, @"\P{L}", String.Empty)
where input
is the input string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.