Solved

reg ex - \$ amount

Posted on 2012-08-15
386 Views
I am looking a pattern to validate sample below
\$1.00
\$10.00
up to \$100,000.00

This pattern below is up to \$99.00
[0-9]?[0-9]\.[0-9][0-9]\$
0
Question by:VBdotnet2005

LVL 74

Expert Comment

Try:

``````^(100|\d\d),\d\d\d\.\d\d\$
``````

...and if you require the leading \$:

``````^\$(100|\d\d),\d\d\d\.\d\d\$
``````
0

Author Comment

sorry, I need this format 1.00 up to 100,000.00
^(100|\d\d),\d\d\d\.\d\d\$
wont't work
0

LVL 74

Accepted Solution

Yes, you're correct...  I'm up way past my bedtime  : \

Corrected:

``````^(?:100,000|(?:(?:(?:\d?\d,)?\d)?\d)?\d)\.00\$
``````

If the digits to the right of the decimal can be something other than zeros, then change the two zeros to "\d\d".
0

LVL 62

Expert Comment

Hi VBdotnet2005;

The following Regex pattern should give you what you need.

``````string pattern = @"^(?:[0-9]{1}\.[0-9]{2}|[0-9]{2}\.[0-9]{2}|[0-9]{3}\.[0-9]{2}|[0-9]{1},[0-9]{3}\.[0-9]{2}|[0-9]{2},[0-9]{3}\.[0-9]{2}|100,000\.00)\$";
``````
0

Author Comment

I am so sorry guys. It does not need to be .00 at the end.  It can be 1.25 or 1.00.
Thank you very much in advance.
0

LVL 62

Expert Comment

Hi VBdotnet2005;

Just take @kaufmed solution and change the last two 00 to \d\d as shown below.

``````^(?:100,000|(?:(?:(?:\d?\d,)?\d)?\d)?\d)\.\d\d\$
``````
0

Featured Post

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…