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
2010-06-16 17:37:35
Too lazy ATM to verify, but don't you need to escape the . ?
Andrew Anderson
2010-06-16 17:38:20
@Andrew: no, inside a character class, `.` has no special meaning.
Bart Kiers
2010-06-16 17:38:58
+6
A:
This should do it
string x = "joe ($3,004.50)" ;
x = Regex.Replace(x, "[^0-9.]", "");
josephj1989
2010-06-16 17:38:28