Match price with a few commas

I want to match values ​​that contain several commas in them. I can match values ​​with only one comma. regex: (\$\d+\,\d+) Example value: $567,76 , but I need to match this value $567,76,87 , but this regex does not work.

+4
source share
3 answers

Try this template, maybe it can help.

 ^\$\d+(,\d+)*$ 

It will fit

 $567 $567,76 $567,76,87 

but not

 $567,76,87, 
+6
source

You can parse the value with double.Parse(currency, NumberStyles.Currency) instead of the usual expression.

+1
source

Try it:

 ^\$\d+(,\d+)+$ 

Some random coincidences;

 $1622,40,749 $37,5844 

 Regex r = new Regex(@"^\$\d+(,\d+)+$"); string[] partNumbers = { "$567,76", "$567,76,87", "$567,76,87," }; foreach (var i in partNumbers) { Console.WriteLine("{0} {1} a valid part number.", i, r.IsMatch(i) ? "is" : "is not"); } 

 $567,76 is a valid part number. $567,76,87 is a valid part number. $567,76,87, is not a valid part number. 

Here is the DEMO .

+1
source

All Articles