tags:

views:

62

answers:

2

I need for text like "joe ($3,004.50)" to be filtered down to 3004.50 but am terrible at regex and can't find a suitable solution. So only numbers and periods should stay - everything else filtered. I use C# and VS.net 2008 framework 3.5

+5  A: 

The regex is:

[^0-9.]

You can cache the regex:

Regex not_num_period = new Regex("[^0-9.]")

then use:

string result = not_num_period.Replace("joe ($3,004.50)", "");

However, you should keep in mind that some cultures have different conventions for writing monetary amounts, such as: 3.004,50.

Matthew Flaschen
Too lazy ATM to verify, but don't you need to escape the . ?
Andrew Anderson
@Andrew: no, inside a character class, `.` has no special meaning.
Bart Kiers
+6  A: 

This should do it

   string x = "joe ($3,004.50)" ;
        x = Regex.Replace(x, "[^0-9.]", "");
josephj1989
Didn't know you could do this, thanks!
VoodooChild
thanks indeed :)
Ready Cent