从C#中的字符串中删除特殊字符列表

时间:2014-04-03 07:40:48

标签: c# regex

我需要将具有特定特殊字符列表的字符串替换为string.Empty。

示例:123~Main到123 Main

特殊字符列表:+ - && || ! (){} [] ^“〜*?:\

我知道我们可以在下面这样做,我们可以有更好的方法,使用一些正则表达式。

> keyword = keyword.Replace("+", string.Empty);
>         keyword = keyword.Replace("&&", string.Empty);
>         keyword = keyword.Replace("||", string.Empty);
>         keyword = keyword.Replace("!", string.Empty);
>         keyword = keyword.Replace("(", string.Empty);
>         keyword = keyword.Replace(")", string.Empty);
>         keyword = keyword.Replace("{", string.Empty);
>         keyword = keyword.Replace("}", string.Empty);
>         keyword = keyword.Replace("[", string.Empty);
>         keyword = keyword.Replace("]", string.Empty);
>         keyword = keyword.Replace("^", string.Empty);
>         keyword = keyword.Replace("~", string.Empty);
>         keyword = keyword.Replace("*", string.Empty);
>         keyword = keyword.Replace("?", string.Empty);
>         keyword = keyword.Replace(":", string.Empty);
>         keyword = keyword.Replace("\\", string.Empty);
>         keyword = keyword.Replace("\"", string.Empty);

提前致谢。

5 个答案:

答案 0 :(得分:4)

您可以动态构建正则表达式 - \+|&&|\|\||!|\(|\)|\{|}|\[|]|\^|~|\*|\?|:|\\|"

string input = "Hello world!";
string[] replaceables = new[] { "+", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "~", "*", "?", ":", "\\", "\"" };
string rxString = string.Join("|", replaceables.Select(s => Regex.Escape(s)));
string output = Regex.Replace(input, rxString, string.Empty);

您也可以像@Robin建议的那样优化正则表达式(尽管生成变得复杂) - [\+!\(\)\{}\[\]\^~\*\?:\\"]|&&|\|\|

string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
                                               .Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
                                                                  : string.Format("[{0}]", string.Join(string.Empty, g.Select(r => r == "]" ? "\\]" : Regex.Escape(r))))));

稍微干净的正则表达式(方括号之间只有metacharacters转义) - [+!(){}[\]\^~*?:\\"]|&&|\|\|

string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
                                               .Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
                                                                  : string.Format("[{0}]", string.Join(string.Empty, g.Select(r => new[] { "]", @"\", "-", "^" }.Contains(r) ? @"\" + r : r)))));

最干净的正则表达式(对转义元字符的要求的额外验证) - [+!(){}[\]^~*?:\\"]|&&|\|\|

string rxString = string.Join("|", replaceables.GroupBy(r => r.Length > 1)
                                               .Select(g => g.Key ? string.Join("|", g.Select(r => Regex.Escape(r)))
                                                                  : string.Format("[{0}]", string.Join(string.Empty, g.Select((r, i) => r == @"\" || r == "]" && g.Count() > 1 || r == "^" && i == 0 || r == "-" && i > 0 ? @"\" + r : r)))));

如果你有像Hello &|&&|& complicated world!这样的字符串,那么你需要多次传递它。

string output = input;
string outputRef = output;
do
{
    outputRef = output;
    output = Regex.Replace(output, rxString, string.Empty);
} while (output != outputRef);
Console.WriteLine(output); // Hello  complicated world

答案 1 :(得分:1)

试试这个......

        //to remove non alphanumeric characters (special characters) from a string?
        public static string removespclchr(string input)
        {
            Regex regex = new Regex("[^a-zA-Z0-9]");
            return regex.Replace(input, "");
        }

string q = Regex.Replace(query, @""|['"",&?%\.*:#/\\-]", " ").Trim();

string replacestr= Regex.Replace(str, "[^a-zA-Z0-9_]+", " ");

答案 2 :(得分:1)

使用LINQ的另一个“漂亮”解决方案:

const string input = "+Hello world!-&&-Hey!";
string[] replaceables = { "+", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "~", "*", "?", ":", "\\", "\"" };
var cleanInput = replaceables.Aggregate(input, (result, item) => result.Replace(item, string.Empty));

但我想使用Regex表达式在性能方面会更有效(特别是如果你的字符串很大)。

答案 3 :(得分:1)

您可以在此处使用StringBuilder

string str = "+ - && || ! ( ) { } [ ] ^ \" ~ * ? : \\";
string[] array = str.Split();

string s = "123 ~Main";

StringBuilder sb = new StringBuilder();
foreach (var c in s)
{
    sb = array.Contains(c.ToString()) ? sb.Append("") : sb.Append(c);
}

Console.WriteLine(sb.ToString()); // 123 Main
Console.ReadLine();

答案 4 :(得分:0)

如果你想如此糟糕地使用替换功能,那就这样做:

string[] replaceables = new[] { "+", "&&", "||", "!", "(", ")", "{", "}", "[", "]", "^", "~", "*", "?", ":", "\\", "\"" };

for(int i = 0; i < replaceables.lenght; i++)
{
  myString = myString.replace(replaceables[i], String.Empty);
}
  • 数组&#34;字符串[]可替换&#34;复制自@Ulugbek Umirov