.NET Compatible RegEx String for Currency

I've seen several questions about Regex so far, and some of them have been very helpful, but I have some problems getting the result I want ...

One question that helped me learn a lot is the following: Regular expression to match numbers with or without commas and decimals

The expression that was asked above is as follows:

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

      

This will allow things like:

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

      

... and don't like:

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

      

Now this is fine, but I don't want to allow:

0.111
.111
0

      

I don't want to allow anything more than 2 decimal places, so 100.333 will not be accepted whereas 100.33 will pass just fine. I also don't want anything to start at 0 in order to get through.

This is my first experience with regex and I understand that stackoverflow gave me some suggestions by asking this question that I was looking at, but I really don't get it right now - if you could help me understand what it is what i need to change and why, i would really appreciate it.

+3


source to share


1 answer


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}

+3


source







All Articles