Remove special characters from a string except whitespace

前端 未结 2 1457
既然无缘
既然无缘 2021-01-14 07:05

I am looking for a regular expression to remove all special characters from a string, except whitespace. And maybe replace all multi- whitespaces with a single whitespace.

2条回答
  •  星月不相逢
    2021-01-14 08:06

    Use the regex [^\w\s] to remove all special characters other than words and white spaces, then replace:

    Regex.Replace("[one@ !two three-four]", "[^\w\s]", "").Replace("  ", " ").Trim
    

提交回复
热议问题