Skip to main content
 首页 » 编程设计

c#之正则表达式模式失败

2024年01月21日31Terrylee

我正在尝试删除字符串中不是字母数字或空格的所有内容,因此我创建了正则表达式

private static Regex _NonAlphaChars = new Regex("[^[A-Za-z0-9 ]]", RegexOptions.Compiled); 

然而当我调用 _NonAlphaChars.Replace("Scott,", "");它返回 "Scott,"
我做错了什么,它与 , 不匹配?

请您参考如下方法:

private static Regex _NonAlphaChars = 
    new Regex("[^A-Za-z0-9 ]", RegexOptions.Compiled);