开发者

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: \’ \*.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜