tags:

views:

78

answers:

3

I have this statement:

String cap = Regex.Replace(winCaption, @"[^\w\.@-]", ""); 

that transforms "Hello | World!?" to "HelloWorld".

But I want to preserve space character, for example: "Hello | World!?" to "Hello  World".

How can I do this?

+3  A: 

just add a space to your set of characters, [^\w.@- ]

var winCaption = "Hello | World!?";
String cap = Regex.Replace(winCaption, @"[^\w\.@\- ]", "");

Note that you have to escape the 'dash' (-) character since it normally is used to denote a range of characters (for instance, [A-Za-z0-9])

James Manning
I try, but I have this error:parsing "[^\w\.- ]" - [x-y] range in reverse order.
Cecco
Sorry, works perfectly!! Thanks James!
Cecco
+1  A: 

Here you go...

string cap = Regex.Replace(winCaption, @"[^\w \.@-]", "");
Ian P
A: 

Try this:

  String cap= Regex.Replace(winCaption, @"[^\w\.@\- ]", "");
Johnny