[ACCEPTED]-Regex for Money-currency

Accepted answer
Score: 31
\d+(,\d{1,2})?

will allow the comma only when you have 12 decimal digits, and allow no comma at all. The 11 question mark means the same as {0,1}, so after 10 the \d+ you have either zero instances (i.e. nothing) or 9 one instance of

,\d{1,2}

As Helen points out correctly, it 8 will suffice to use a non-capturing group, as 7 in

\d+(?:,\d{1,2})?

The additional ?: means that the parentheses 6 are only meant to group the ,\d{1,2} part for use 5 by the question mark, but that there is 4 no need to remember what was matched within 3 these parenthesis. Since this means less 2 work for the regex enginge, you get a performance 1 boost.

Score: 7

We use this very liberal regular expression 1 for money validation:

new Regex(@"^\-?\(?\$?\s*\-?\s*\(?(((\d{1,3}((\,\d{3})*|\d*))?(\.\d{1,4})?)|((\d{1,3}((\,\d{3})*|\d*))(\.\d{0,4})?))\)?$");

It allows all these: $0, 0, (0.0000), .1, .01, .0001, $.1, $.01, $.0001, ($.1), ($.01), $(.0001), 0.1, 0.01, 0.0001, 1., 1111., 1,111., 1, 1.00, 1,000.00, $1, $1.00, $1,000.00, $ 1.0000, $ 1.0000, $ 1,000.0000, -1, -1.00, -1,000.00, -$1, -$1.00, -$1,000.00, -$ 1, -$ 1.00, -$ 1,000.00, $-1, $-1.00, $-1,000.00, $(1), $(1.00), $(1,000.00), $ (1), $ (1.00), $ (1,000.00), ($1), ($1.00), ($1,000.00)

Score: 3

i used this one in javascript: may be of use for you 1 in c#

var entered = '10.00';
var regex = /^\d+(?:\.\d{2})?$/; // starts with N digits optional ".\d\d"
console.log(entered.match(regex));

More Related questions