Search code examples
javascriptregexzurb-foundation-5

RegEx string compatible with .NET for currency


I've seen a few questions about Regex so far, and some were very helpful, but I'm having some trouble getting the result that I need...

One of the questions that has helped me learn a lot is this one: Regular expression to match numbers with or without commas and decimals in text

The expression that was privided in the above question is :

(:?^|\s)((?:[1-9]\d{0,2}(?:(?:,\d{3})*|\d*)|0|(?=\.))(?:\.\d*[1-9])?)(?!\S)

It will allow things like:

100,000
999.999
90.0009
1,000,023.999
0.111
.111
0

...and not like:

1,1,1.111
000,001.111
999.
0.
111.110000
1.1.1.111
9.909,888

Now that's fine, but I do not want to allow:

0.111
.111
0

I don't want to allow anything more than 2 decimal places, so 100.333 wouldn't be accepted whereas 100.33 would pass just fine. I also don't want anything starting with 0 to be passed through.

This is my first time dealing with regex and I understand stackoverflow gave me a few suggestions while asking this question which I've looked at, but I really do not understand this right now -- if you could help me understand what it is that I need to change and why, I would really appreciate that.


Solution

  • You can use the following:

    (:?^|\s)((?:[1-9]\d{0,2}(?:(?:,\d{3})*|\d*)|(?=\.))(?:\.\d{0,2})?)(?!\S)
                                               ^              ^^^^^
    

    Change \d*[0-9] to \d{0,2}