我想删除给定字符串中除字母和数字之外的所有字符。我使用了下面的模式,但它仍然返回字符串而没有任何变化。
Regex rex = new Regex("/[^a-zA-Z0-9]+/");
Response.Write(rex.Replace("asd123!-<>@;',.", ""));
它假设返回"asd123"
正则表达式对我来说就像外星语言,我不知道如何解决这个问题。
由于
答案 0 :(得分:5)
在C#中,您不需要使用/
个字符分隔正则表达式模式。
试试这个:
Regex rex = new Regex("[^a-zA-Z0-9]+");
Response.Write(rex.Replace("asd123!-<>@;',.", ""));
答案 1 :(得分:4)
这对我有用
string str = "a@4( asd1";
Regex rex = new Regex(@"[^a-zA-Z0-9]+");
System.Console.WriteLine(rex.Replace(str, ""));
答案 2 :(得分:0)
使用C#,你的正则表达式周围不需要斜杠(/
):
Regex rex = new Regex("[^a-zA-Z0-9]+");
Response.Write(rex.Replace("asd123!-<>@;',.", ""));