Regex to Strip Special Characters
I am trying to use regex.replace to strip out unwanted characters, but I need to account for spaces:
string asdf = "doésn't work?";
string regie = @"([{}\(\)\^$&._%#!@=<>:;,~`'\’ \*\?\/\+\|\[\\\\]|\]|\-)";
Response.Write(Regex.Replace(asdf,regie,"").Replace(" ","-"));
returns doésntwork instead of doésnt-work
开发者_StackOverflow社区Ideas?
Thanks!
Your regular expression includes a space, so the space gets stripped out before the string.Replace
is called.
string regie = @"([{}\(\)\^$&._%#!@=<>:;,~`'\’ \*\?\/\+\|\[\\\\]|\]|\-)";
^ here
Remove it from the regular expression and your code should do what you expect:
string regie = @"([{}\(\)\^$&._%#!@=<>:;,~`'\’\*\?\/\+\|\[\\\\]|\]|\-)";
You have a space inside your regex, right here: \’ \*
.
精彩评论