What is the "Best" US Currency Register?

A quick search for a currency regex causes a lot of results .
Does MSDN use ^ -? \ D + (\. \ D {2})? $

The problem with choosing one of them is that the regular expression is hard to check without checking all cases of edges. I could spend a lot of time on this, as I am sure that hundreds of other developers have already done it.

So ... Does anyone have a regex for the US currency that has been thoroughly tested?

My only requirement is that the matching string is US Currency and parses System.Decimal :

 [ws] [sign] [digits,] digits [.fractional-digits] [ws] 

 Elements in square brackets ([and]) are optional. 
 The following table describes each element. 

 ELEMENT DESCRIPTION
 ws Optional white space.
 sign An optional sign.
 digits A sequence of digits ranging from 0 to 9.
 , A culture-specific thousands separator symbol.
 .  A culture-specific decimal point symbol.
 fractional-digits A sequence of digits ranging from 0 to 9. 
+43
regex currency
Dec 09 '08 at 20:05
source share
10 answers

here are some things from the creators of Regex Buddy. They came from the library, so I'm sure they were thoroughly tested.

Number: currency amount (cents required) Additional thousands separators; mandatory double-digit share

Match; JGsoft: ^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$ 

Number: currency amount (optional cent) Additional thousands separators; optional double-digit fraction

 Match; JGsoft: ^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$ 

Number: amount in US and EU currencies (cents optional) Can be used in the form of 123,456.78 in the US style and in the European style 123,456,78. Additional thousands separators; optional double-digit fraction

 Match; JGsoft: ^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$ 
+66
Dec 9 '08 at 20:57
source share

I found this regular expression in a string at www.RegExLib.com by Kirk Fuller, Gregg Durishan

I have successfully used it over the past few years.

 "^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$" 
+12
Dec 09 '08 at 21:20
source share

Not fully tested (I just wrote this!), But seems to behave correctly:

 ^-?(?:0|[1-9]\d{0,2}(?:,?\d{3})*)(?:\.\d+)?$ 

Test suite:

 0 1 33 555 4,656 4656 99,785 125,944 7,994,169 7994169 0.00 1.0 33.78795 555.12 4,656.489 99,785.01 125,944.100 -7,994,169 -7994169.23 // Borderline... Wrong: 000 01 3,3 5. 555, ,656 99,78,5 1,25,944 --7,994,169 0.0,0 .10 33.787,95 4.656.489 99.785,01 1-125,944.1 -7,994E169 

Note. Your System.Decimal depends on the language, it is difficult to do in the regular expression, except, perhaps, when creating it. I suggested that the numbers are grouped in three, even if in some cultures (locales) there are different rules.
It is trivial to add spaces there.

+6
Dec 09 '08 at 21:52
source share

Keng's answer is perfect, I just want to add that to work with 1 or 2 decimal places (for the third version):

 "^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{1})?|(?:,[0-9]{3})*(?:\.[0-9]{1,2})?|(?:\.[0-9]{3})*(?:,[0-9]{1,2})?)$ 

NET FIDDLE: https://dotnetfiddle.net/1mUpX2

+2
Dec 15 '14 at 19:16
source share

This question is several years old, so I wanted to give an updated answer.

I used jQuery InputMask and it works great for masking input / format (like phone numbers, etc.), but actually it does NOT work well for currencies from my experience.

For currency, I highly recommend the autoNumeric jQuery plugin. He is well-groomed, and they basically “thought about everything” that I could want for the currency.

I really use a combination of both of these plugins to format phone numbers, number formats (ISBN, etc.), as well as currencies (mainly in the USA).

Keep in mind that jquery.inputmask is mainly concerned with controlling the format of values, while autoNumeric is specifically controlling the format of the currency.

+1
Jun 12 '16 at 2:54 on
source share

I also looked at this and came to the conclusion that it is best to create a regular expression based on the current culture. We can use

 CurrencyPositivePattern CurrencyGroupSeparator CurrencyDecimalSeparator 

NumberFormatInfo properties to get the required format.

Edit: something like this

 NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat; // Assign needed property values to variables. string currencySymbol = nfi.CurrencySymbol; bool symbolPrecedesIfPositive = nfi.CurrencyPositivePattern % 2 == 0; string groupSeparator = nfi.CurrencyGroupSeparator; string decimalSeparator = nfi.CurrencyDecimalSeparator; // Form regular expression pattern. string pattern = Regex.Escape( symbolPrecedesIfPositive ? currencySymbol : "") + @"\s*[-+]?" + "([0-9]{0,3}(" + groupSeparator + "[0-9]{3})*(" + Regex.Escape(decimalSeparator) + "[0-9]+)?)" + (! symbolPrecedesIfPositive ? currencySymbol : ""); 

refer - http://msdn.microsoft.com/en-us/library/hs600312.aspx

0
Aug 25 2018-11-11T00:
source share

I use the following regex to check the currency:

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

You can also enable an extra dollar sign:

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

You can easily add testing for parentheses instead of a character by adding

 \( and \) 
0
Nov 29 '11 at 13:15
source share

I have had success with this (taking bits and parts from some of the regular expressions above). Only processes up to a thousand, but it shouldn't be too hard to extend it

 case class CurrencyValue(dollars:Int,cents:Int) def cents = """[\.\,]""".r ~> """\d{0,2}""".r ^^ { _.toInt } def dollarAmount: Parser[Int] = """[1-9]{1}[0-9]{0,2}""".r ~ opt( """[\.\,]""".r ~> """\d{3}""".r) ^^ { case x ~ Some(y) => x.toInt * 1000 + y.toInt case x ~ None => x.toInt } def usCurrencyParser = """(\$\s*)?""".r ~> dollarAmount ~ opt(cents) <~ opt( """(?i)dollars?""".r) ^^ { case d ~ Some(change) => CurrencyValue(d, change) case d ~ None => CurrencyValue(d, 0) } 
0
Dec 12 '12 at 17:35
source share

If you want to take into account a human error, you can make the regular expression more forgiving when comparing the currency. I used the second regular expression of Keng 2 and made it a little more robust to allow for typos.

 \$\ ?[+-]?[0-9]{1,3}(?:,?[0-9])*(?:\.[0-9]{1,2})? 

This will match any of these correct or distorted currency digits, but will not raise any extra flop at the end after a space:

 $46,48382 $4,648,382 $ 4,648,382 $4,648,382.20 $4,648,382.2 $4,6483,82.20 $46,48382 70.25PD $ 46,48382 70.25PD 
0
Jan 05 '13 at 17:15
source share

This is what I use:

Without + or -

 ^\$\d{1,3}\.[0-9]{2}$|^\$(\d{1,3},)+\d{3}\.[0-9]{2}$ 

With additional presenters + or -

 ^[+-]?\$\d{1,3}\.[0-9]{2}$|^[+-]?\$(\d{1,3},)+\d{3}\.[0-9]{2}$ 

net fiddle: https://jsfiddle.net/compsult/9of63cwk/12/

0
Jul 07 '16 at 21:41
source share



All Articles