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

前端 未结 6 522
轮回少年
轮回少年 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条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-29 08:20

    This should be the answer to your question:

    Regex.Matches("Little?~ birds! like to@ sing##", "[~!@#$%^&*()_+{}:\"<>?]").Count
    

    Count should return 6 matches, change the sentence to other variable or something else.

    You can find more info about regex expressions here: http://www.zytrax.com/tech/web/regex.htm

    Best Regards!

提交回复
热议问题