开发者

Regex to Strip Special Characters

开发者 https://www.devze.com 2022-12-28 07:32 出处:网络
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?\";

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

精彩评论

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

关注公众号