正则表达式删除特殊字符

use*_*760 14 c# regex

我们需要一个C#函数,它将从字符串中删除所有特殊字符.

此外,是否可以将"乔治"更改为"乔治"(删除单引号和字符)?

Rya*_*sen 43

此方法将删除除字母,数字和空格之外的所有内容.它还将删除角色s后面的任何"或".

public static string RemoveSpecialCharacters(string input)
{
    Regex r = new Regex("(?:[^a-z0-9 ]|(?<=['\"])s)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
    return r.Replace(input, String.Empty);
}
Run Code Online (Sandbox Code Playgroud)


Has*_*ash 6

public static string RemoveSpecialCharacters(string input)
{    
    Regex r = new Regex(
                  "(?:[^a-zA-Z0-9 ]|(?<=['\"])s)",
                  RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
    return r.Replace(input, String.Empty);    
}
Run Code Online (Sandbox Code Playgroud)

瑞安的回答是正确的。只需添加A-Z以及许多人需要它即可。