tags:

views:

52

answers:

2

I've been scratching my head trying to figure out how to use Regex.Replace to take an arbitrary string and return a string that consists of only the alpha-numeric characters of the original string (all white space and punctuation removed).

Any ideas?

+3  A: 

You could use linq:

string alphanumeric = new String(original.Where(c => Char.IsLetterOrDigit(c)).ToArray());
Lee
+7  A: 
var result = Regex.Replace(input, @"[^a-zA-Z0-9]", "");
František Žiačik
Can we can make this regex shorter by using \W instead of "[^a-zA-Z0-9]" ???
Shekhar