Regex to remove all special characters from string?

前端 未结 9 1223
谎友^
谎友^ 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 15:42

    Depending on your definition of "special character", I think "[^a-zA-Z0-9]" would probably do the trick. That would find anything that is not a small letter, a capital letter, or a digit.

    0 讨论(0)
  • 2020-12-04 15:43

    If you don't want to use Regex then another option is to use

    char.IsLetterOrDigit
    

    You can use this to loop through each char of the string and only return if true.

    0 讨论(0)
  • 2020-12-04 15:48
    public static string Letters(this string input)
    {
        return string.Concat(input.Where(x => char.IsLetter(x) && !char.IsSymbol(x) && !char.IsWhiteSpace(x)));
    }
    
    0 讨论(0)
  • 2020-12-04 15:49

    For my purposes I wanted all English ASCII chars, so this worked.

    html = Regex.Replace(html, "[^\x00-\x80]+", "")
    
    0 讨论(0)
  • 2020-12-04 15:53

    [^a-zA-Z0-9] is a character class matches any non-alphanumeric characters.

    Alternatively, [^\w\d] does the same thing.

    Usage:

    string regExp = "[^\w\d]";
    string tmp = Regex.Replace(n, regExp, "");
    
    0 讨论(0)
  • 2020-12-04 15:57

    This should do it:

    [^a-zA-Z0-9]
    

    Basically it matches all non-alphanumeric characters.

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