Regex to remove all special characters from string?

前端 未结 9 1224
谎友^
谎友^ 2020-12-04 15:15

I\'m completely incapable of regular expressions, and so I need some help with a problem that I think would best be solved by using regular expressions.

I have list

相关标签:
9条回答
  • 2020-12-04 16:00

    It really depends on your definition of special characters. I find that a whitelist rather than a blacklist is the best approach in most situations:

    tmp = Regex.Replace(n, "[^0-9a-zA-Z]+", "");
    

    You should be careful with your current approach because the following two items will be converted to the same string and will therefore be indistinguishable:

    "TRA-12:123"
    "TRA-121:23"
    
    0 讨论(0)
  • 2020-12-04 16:02
    tmp = Regex.Replace(n, @"\W+", "");
    

    \w matches letters, digits, and underscores, \W is the negated version.

    0 讨论(0)
  • 2020-12-04 16:03

    You can use:

    string regExp = "\\W";
    

    This is equivalent to Daniel's "[^a-zA-Z0-9]"

    \W matches any nonword character. Equivalent to the Unicode categories [^\p{Ll}\p{Lu}\p{Lt}\p{Lo}\p{Nd}\p{Pc}].

    0 讨论(0)
提交回复
热议问题