Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove special characters from a string except whitespace

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.

For example "[one@ !two three-four]" should become "one two three-four"

I tried using str = Regex.Replace(strTemp, "^[-_,A-Za-z0-9]$", "").Trim() but it does not work. I also tried few more but they either get rid of the whitespace or do not replace all the special characters.

like image 318
Coding Duchess Avatar asked Jul 19 '26 09:07

Coding Duchess


1 Answers

[ ](?=[ ])|[^-_,A-Za-z0-9 ]+

Try this.See demo.Replace by empty string.See demo.

http://regex101.com/r/lZ5mN8/69

like image 177
vks Avatar answered Jul 21 '26 23:07

vks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!