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

前端 未结 6 571
轮回少年
轮回少年 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
    2021-01-29 08:15

    It's been a while and I needed a similar answer for handling password validation. Pretty much what VITA said, but here was my specific take for others needing it for the same thing:

    var pwdSpecialCharacterCount = Regex.Matches(item, "[~!@#$%^&*()_+{}:\"<>?]").Count;
    var pwdMinNumericalCharacters = Regex.Matches(item, "[0-9]").Count;
    var pwdMinUpperCaseCharacters = Regex.Matches(item, "[A-Z]").Count;
    var pwdMinLowerCaseCharacters = Regex.Matches(item, "[a-z]").Count;
    

提交回复
热议问题