tags:

views:

76

answers:

1

Hello,

I have a document that I'm parsing text out of - I'm trying to figure out how to use this RegEx expression to take out everything that isn't alphanumeric, but I want to keep quotes, ampersands and colons/semi-colons.

               s = Regex.Replace(s, @"[^\w-]+", " ");

How can I add a replace all of these "except these" pattern here?

Thank you!

+3  A: 

Just put all those exceptional cases into the character class.

s = Regex.Replace(s, @"[^\w'""&:;-]+", " ");
KennyTM
I think you need to escape that " even with the literal string identifier. Dont remember what the escape is, I think its """
GrayWizardx
Thanks guys, worked great. I didn't need to add any escape characters to the sequence - maybe it was added in the sample above that I used.