views:

5454

answers:

7

I need a regex suitable for c# that'll validate a number if it matches:

 $1,000,000.150
 $10000000.199
 $10000 
 1,000,000.150
 100000.123
 10000

or the negative equivalents

Anyone able to help?

+2  A: 

^\$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{1,3})?|\d{1,3}(.\d{2})?)$

csmba
oops, spoke to soon, it seems to die on negative numbers
nailitdown
+1  A: 

Try this one. It may need some fine tuning to only allow for a single decimal point, but it does match your test cases. I hope this helps.

[$\d,.]+
James
+8  A: 

Why bother with Regex?

float num;
bool isValid = float.TryParse(str, 
    NumberStyles.Currency,
    CultureInfo.GetCultureInfo("en-US"), // cached
    out num);
ssg
+1 - exactly what I was getting at with the question in my comment. I was just waiting for an answer before I suggested that very trick! :)
Matt Hamilton
good tip, but I should've mentioned in the question - using a third party control so i'm limited to specifying either "number" or "regex=XXXX"
nailitdown
It's like cheating when I'm using .NET framework :) I love it.
ssg
nailitown: sorry. when i saw "c#" i thought it was a coding question. are you sure control doesn't have a "currency" mode?
ssg
ssg: i'm sure the regex is just as useful in any other c# context... positive on the control, it's not the most flexible thing in the world.
nailitdown
i feel your pain. luckily you got your answer :)
ssg
+1 for a good -proper- solution
nailitdown
+1 for the one and only correct solution that deals with all cases. However, I prefer the invariant culture for parsing, or just the current UI culture if the value is to be displayed to or entered by the user.
OregonGhost
Good answer, not accepted because I specifically needed regex.
nailitdown
yeah, figured that out later :) i'll leave it here though in case someone else needs it.
ssg
+2  A: 

You can use csmba's regex if you make one slight modification to it.

^\$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{0,3})?|\d{1,3}(.\d{2})?)$

gwhitake
cheers - csamba's appeared to work for me, is there a bug this mod takes care of?
nailitdown
marked as answer because it actually provided a working regex
nailitdown
+3  A: 

I think ssg is right. It's not a really good use of Regex, especially if your software has to deal with non-US centric data entry.

For instance, if the currency symbol is the Euro, or the Japanese Yen or the British Pound any of the other dozen currency symbols out there?

What about number formatting rules?

In the US you would enter 1,000,000.00 but in France, this should be 1.000.000,00. Other countries allow spacing between digit-grouping...

If you use a straight Regex without taking the Culture into account, then you're never going to validate successfully unless you're 100% sure your software will never ever be used in a non-US centric context.

Renaud Bompuis
+1 good points to consider
nailitdown
+1 ditto--good points.
James
A: 

Be careful with floats. Eventually you will hit a case such as 0.01 represented as 0.00999999. Strings or integers are better to use.

Brian Carlton
A: 

Use this regular expression for US currency \$(\d)*\d Matches $300,$12900 Non-Match $12900.00