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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Currency Reg Expression

Status
Not open for further replies.

tcerv79

Programmer
Mar 26, 2009
16
US
Currently the regular expression we are using to define currency is not returning results where we expect. If we receive a currency that has text in it, this does not fail. Does anyone have a better reg exp they can suggest to replace the below?
Code:
if(Currency !~ /^((|-)[0-9]+\.[0-9][0-9])$/ && Currency !="" && Currency !~ "^[ \t]*$" && Currency !~ "^[ \t]")
          {BadCurrencyCount[x]++;       #Counts number of bad currencies with respect to each column
           BadCurrencyCounter++;	#Counts total number of bad currencies, used for pass/fail check
           BadCurrencyArray[BadCurrencyCounter]=Currency; #Keeps examples of bad currencies
CurrencyColumn[CURRENCYFIELDS[x]] = (BadCurrencyCount[x] "," CURRENCYFIELDS[x]); #Done to link the actual column # to the array subscript
 
While I can see some improvements (there is some redundancy there) it does actually seem to work for me except for the case where the field begins with spaces or tabs; can you give some examples of values that pass the test when they shouldn't? These were my tests:

Code:
[COLOR=red] is good[/color]
0.10 is good
[COLOR=red] fred is good[/color]
125.0e is counterfeit!
.03 is counterfeit!
-0.10 is good
fred is counterfeit!
12a.03 is counterfeit!
+0.10 is counterfeit!
[COLOR=red] 123.0 is good[/color]
a125.08 is counterfeit!
0.1 is counterfeit!
100.10 is good
123.0 is counterfeit!

Currency != "" is redundant because Currency !~ "^[ \t]*$" catches that case too. Also (|-) can be replaced by simply -? (which is also more portable), and I would combine all of these into one expression ultimately, but can you clarify the treatment of white space; are you happy if the field is empty? If it contains tabs and/or spaces is it considered empty? And if it is a valid value prefixed by tabs and spaces, is that fine?

Annihilannic.
 
Thank you for pointing out the redundancy. That helps! What is the difference then between (|-) and -? , as I am unsure. Essentially if the field NEEDS to be in this format 123.00 or -123.00 anything else(blanks or empty strings or whitespace, $ within the field, or a-zA-Z TEXT) should NOT be in the field.
 
? means that the previous expression 0 or more times. There's no difference between (|-) and -? except that awk on some platforms (e.g. HP-UX, Solaris) doesn't support (|-); I guess they don't like the "empty" expression.

You haven't given an example of a string that passes your regexp test when it shoudln't?

Annihilannic.
 
The strings that should PASS this check are as follows:
0.00
-1.23
1.23
10.20
100.23
0.23
-0.23
WHAT SHOULD NOT PASS THIS EXPRESSION IS
000.00
0001.23
-00012.23
-0000.23
0000.43

Any help would be FANTASTIC
 
Try this:

Code:
                if (Currency !~ /^(-?([1-9][0-9]*|0)\.[0-9][0-9])$/ && Currency !~ /^[ \t]*$/ && Currency !~ /^[ \t]/) {

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top