Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Money Regex

Status
Not open for further replies.

Skie

Programmer
Jun 21, 2004
475
0
0
US
I have the following regular expression (fairly simple) that is a good start, but I want to improve it and haven't found a good site to explain how to improve it.
Code:
^(0|[1-9]\d{0,2}(,?\d{3})*)(\.\d{1,2})?$

I'd like it so that if the comma separator is used, then it has to continue to be used.

I'd like to be able to put a minus either at the beginning or end of the amount, but not both.

I'd also like it so that if the amount is for cents, the preceding zero would not be required, but the decimal with 1-2 digits would be.

Any help with this is greatly appreciate. Also, if you know a good reference for learning regex that'd be great too.
 
Do you have to do it all in one go? Couldn't you use one regex for comma-ed amounts and another for comma-less.

Personally, I distrust regex (my background as a non-unix guy showing here), and avoid putting too much functionality in a single line of regex gibberish.

Brian Begy
Chief Database Guy
Chicago Data Solutions
 
Have you looked at Regexlib? You might be able find something someone has already created that will suit your needs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top