Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I have a regular expression that I am trying to do check in case the user doesn't type the correct currency format, but my reg. exp. seems not to be picking up on my format, here is the code and thanks for the help!

# sample matches: $3,023,123.34 / 9,876,453 / 123456.78 # sample non-matches: 4,33,234.34 / $1.234 / abc #"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9] +)?$" #"^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9] +)?$" if ($payment=~/^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9] ++)(.[0-9][0-9])?$/g) { print "*match*$payment*match*<br>"; }else{print "No match";}

Replies are listed 'Best First'.
Re: Regular Expression Currency Check
by Joost (Canon) on Jun 02, 2005 at 13:15 UTC
Re: Regular Expression Currency Check
by japhy (Canon) on Jun 02, 2005 at 13:27 UTC
    It works fine for me. I changed the char classes to \d, but that doesn't change anything. I backslashed the '.' so it matches a literal '.' and not any character. Perhaps your string has whitespace around it that you're not expecting?
    /^\s*\$?(\d{1,3},(\d{3},)*\d{3}|\d+)(\.\d\d)?\s*$/
    Try that...

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
      Hi I changed your code to:
      /^\s*\$?(\d{1,3},(\d{3},)*\d{3}|\d+)(\.\d{2})?\s*$

      To enforce the two decimal digits, but it's not picking up on it, it should like that right?
        I think I got it, I just would like to add the last check, if value is more them 99999.99 to the reg. exp. as well, and here is the code so far:

        if ( ($payment=~/^\s*\$?(\d{1,3},(\d{3},)*\d{3}|\d+)(\.\d +{2})\s*$/) || ($payment > 99999.99) ) { print "^*$1-$2-$3-match*$payment*match*^<br>"; }else{print "No match";}
Re: Regular Expression Currency Check
by Tomtom (Scribe) on Jun 02, 2005 at 13:49 UTC
    Try this :
    /^\$?(?:\d{1,3},?(?=\d{3}))*[0-9]{1,3}(?:\.\d{2})?$/
    I added (?=\d{3}) for a look ahead, otherwise, it matches things like "2,34,3.56". If you have a comma, you're supposed to have three digits just afterwards. ( I hope I'm being clear enough ).
    Moreover, you could add "?:" since you're not capturing any pattern.
Re: Regular Expression Currency Check
by cool_jr256 (Acolyte) on Jun 02, 2005 at 13:55 UTC
    Quick update... Try this, basically instead of matching for the correct format match for incorrect:
    if($payment !~ m/[0-9]\.[0-9]{3}|[a-z]|\,[0-9]{2}\,|\,[0-9]{2}\./) { print "*match*$payment*match\n";
    there...
      As a side note, the reason you could not force the 2 decimal places is because \.[0-9]{2} still holds true if the decimal places is ".123" or ".1234" etc...
        This code still matchs on 1,00000.00
        Where is the problem here?
        if ( ($payment=~/^\s*\$?(?:\d{1,3},?(?=\d{3}))*[0-9]{1,3}(?:\.\d{2} +)?$/) && ($payment <= 99999.99) ) {print "right";}else{print "wrong";}
        So, your code above fixed the problem?
Re: Regular Expression Currency Check
by GrandFather (Saint) on Jun 03, 2005 at 00:41 UTC
    Moving the comma from the end of the first digit group match to the start of the second digit group tidys things up a little:
      /^\$?(\d{1,3}(,\d{3})*|\d+)(\.\d\d)?$/ Update: Fix the ReadMore :-)
    Raise your eyes and look out of the rut.
Re: Regular Expression Currency Check
by thcsoft (Monk) on Jun 02, 2005 at 13:15 UTC
    perldoc perlretut

    language is a virus from outer space.
      And I just have to ask how to enforce the value to have to digits after de \. like 100.00 or 100,000.00 and so forth.