RegExLib.com - The first Regular Expression Library on the Web!

Please support RegExLib Sponsors

Sponsors

Regular Expression Details

Title Test Find Pattern Title
Expression
(?n:(^\$?(?!0,?\d)\d{1,3}(?=(?<1>,)|(?<1>))(\k<1>\d{3})*(\.\d\d)?)$)
Description
Regular expression for validating a US currency string field. Matches an unlimited number of digits to the left of an optional decimal point. Digits to the left of the decimal point can optionally be formatted with commas, in standard US currency format. If the decimal point is present, it must be followed by exactly two digits to the right. Matches an optional preceding dollar sign. Uses regex lookahead to preclude leading zeros and to match the optional formatting comma.
Matches
$3,023,123.34 | 9,876 | 123456.78
Non-Matches
0.002 | $01.00 | ###1.00
Author Rating: Not yet rated. Jerry Schmersahl
Source
Your Rating
Bad Good

Enter New Comment

Title

Name

Comment

Spammers suck - we apologize. Please enter the text shown below to enable your comment (not case sensitive - try as many times as you need to if the first ones are too hard):

Existing User Comments

Title: doesn't work in regular expression validator
Name: doesn't work in regular expression validator
Date: 10/25/2007 5:58:14 AM
Comment:
this is nice function but when put in regular expression validator it gets parsed and becomes "(?n:(^\\$?(?!0,?\\d)\\d{1,3}(?=(?<1>,)|(?<1>))(\\k<1>\\d{3})*(\\.\\d\\d)?)$)" and thus doesn't perform properly


Title: Good Stuff (but not for client-side)
Name: Jacque Harper
Date: 11/9/2004 11:26:16 AM
Comment:
I learned a great deal while studying this pattern, and offer thanks to the author for providing it. A caveat for less-experienced developers: this may not work reliably when it runs client-side. I ended up using (^\$?(?!0,?\d)\d{1,3}(,?\d{3})*(\.\d\d)?)$ in my application, and had good results.


Title: Expression does not work for me ???
Name: Angela
Date: 2/12/2004 9:42:13 AM
Comment:
Hello, I found the expression to be just what i need for a project i am working on. I am new at programming and so i tried this expression and the error message will fire if i enter a digit or a letter, it will only match a space. what am i doing wrong?? Any help will be greatly appreciated. Thanks


Title: Nice one
Name: Darren Neimke
Date: 1/25/2004 3:32:42 AM
Comment:
There's some really nice stuff in there. I liked the use of (?!0,?\d) to enforce values at the front of the string and also \k to ensure that, once apostrophe's have been started they must be continued throughout.


Copyright © 2001-2024, RegexAdvice.com | ASP.NET Tutorials