Regular Expressions

BurncycleBurncycle Join Date: 2002-11-24 Member: 9759Members, NS1 Playtester
I'm writing a linux script for class, and am running into trouble with some of the logic of the regular expressions here...

I have the date done (it has to follow a mm/dd/yyyy format, so I know exactly how many numbers are needed) but now I need one for currency that needs to follow the $2943.00 format (always 2 decimals, always $ in front, commas optional, anything else is a no match)

The problem is, there could technically be an infinite number of numbers there. As long as they're preceded by a $ and ended with a . and cents, its ok

I'm not sure exactly how to allow any number of digits, and it's probably something really simple that I just don't see <!--emo&:(--><img src='http://www.unknownworlds.com/forums/html//emoticons/sad-fix.gif' border='0' style='vertical-align:middle' alt='sad-fix.gif' /><!--endemo-->

Here's a few examples...
$1,000.00 Match
$1000.00 Match
$50.24 Match
4,000.10 No match (no $)
$800 No Match (no cents)

Comments

  • Dorian_GrayDorian_Gray Join Date: 2004-02-15 Member: 26581Members, Constellation
    edited September 2004
    Howabout:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->^[$]([0-9]+[.][0-9]{2})$<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    The + means one or more of the previous character set. This might be slightly off, but should be the general idea you're looking for.
  • BurncycleBurncycle Join Date: 2002-11-24 Member: 9759Members, NS1 Playtester
    <!--QuoteBegin--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> </td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->The + means one or more of the previous character set<!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->

    Ah thats exactly what I was having trouble with

    Thanks a bunch!
  • CrystalSnakeCrystalSnake Join Date: 2002-01-27 Member: 110Members
    <!--QuoteBegin-Dorian Gray+Sep 18 2004, 09:25 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Dorian Gray @ Sep 18 2004, 09:25 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Howabout:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->^[$]([0-9]+[.][0-9]{2})$<!--c2--></td></tr></table><div class='postcolor'><!--ec2--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    What about commas, though?
    You know, the ones that separate groups of three digits, as in:
    $1,000,000.00
  • SkulkBaitSkulkBait Join Date: 2003-02-11 Member: 13423Members
    edited September 2004
    <!--QuoteBegin-CrystalSnake+Sep 18 2004, 04:43 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Sep 18 2004, 04:43 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-Dorian Gray+Sep 18 2004, 09:25 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Dorian Gray @ Sep 18 2004, 09:25 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Howabout:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->^[$]([0-9]+[.][0-9]{2})$<!--c2--></td></tr></table><div class='postcolor'><!--ec2--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    What about commas, though?
    You know, the ones that separate groups of three digits, as in:
    $1,000,000.00 <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    this might work:

    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->^[$](([0-9]+,*)+[.][0-9]{2})$<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->
  • Dorian_GrayDorian_Gray Join Date: 2004-02-15 Member: 26581Members, Constellation
    edited September 2004
    <!--QuoteBegin-CrystalSnake+Sep 18 2004, 09:43 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (CrystalSnake @ Sep 18 2004, 09:43 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin--> <!--QuoteBegin-Dorian Gray+Sep 18 2004, 09:25 PM--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>QUOTE</b> (Dorian Gray @ Sep 18 2004, 09:25 PM)</td></tr><tr><td id='QUOTE'><!--QuoteEBegin-->Howabout:
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->^[$]([0-9]+[.][0-9]{2})$<!--c2--></td></tr></table><div class='postcolor'><!--ec2--><!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    What about commas, though?
    You know, the ones that separate groups of three digits, as in:
    $1,000,000.00 <!--QuoteEnd--></td></tr></table><div class='postcolor'><!--QuoteEEnd-->
    <!--c1--></div><table border='0' align='center' width='95%' cellpadding='3' cellspacing='1'><tr><td><b>CODE</b> </td></tr><tr><td id='CODE'><!--ec1-->^(\$([0-9]{1,3}\,)([0-9]{3}\,)*[0-9]{3}[.][0-9]{2})|(\$[0-9]+[.][0-9]{2})$<!--c2--></td></tr></table><div class='postcolor'><!--ec2-->

    That one should work with commas too <!--emo&:p--><img src='http://www.unknownworlds.com/forums/html//emoticons/tounge.gif' border='0' style='vertical-align:middle' alt='tounge.gif' /><!--endemo-->

    The first group can have 1-3 digits then a comma, then all subsequent groups have to have 3 digits then a comma and the last group has 3 digits then a period, then 2 digits. The second case is if the entire thing is any number of digits, with a period and 2 digits at the end. Oh, and a dollar sign at the front.

    Edit: Skulkbait, that new one you have also accepts stuff like "$1,0000,,,,,0.00" (I just ran it through a testing thing)
Sign In or Register to comment.