tags:

views:

58

answers:

2

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

Ideas?

Thanks!

+4  A: 

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 = @"([{}\(\)\^$&._%#!@=<>:;,~`'\’\*\?\/\+\|\[\\\\]|\]|\-)";
Mark Byers
A: 

You have a space inside your regex, right here: \’ \*.

John Kugelman