How to get the count of only special character in a string using Regex?

前端 未结 6 521
轮回少年
轮回少年 2021-01-29 07:44

If my input string is ~!@#$%^&*()_+{}:\"<>?

How do I get the count of each special character using Regex? For example:

Regex.Match         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 08:28

    Regex is not the best way to do this. here is the Linq based solution

    string chars = "~!@#$%^&*()_+{}:\"<>?";
    foreach (var item in chars.Where(x=> !char.IsLetterOrDigit(x)).GroupBy(x => x))
    {
        Console.WriteLine(string.Format("{0},{1}",item.Key,item.Count()));
    }
    

    I understand that you need to count each spl character count. Correct me If am mistaken.

提交回复
热议问题