I figure that sometimes these things get overly complex to the detriment of clarity. Maybe I'm guilty below as I allowed for Australian dollars vs US dollars...

Don't be afraid to use the Perl regex engine in a couple of lines! The speed of this thing in Perl 5.10 vs Perl 5.6 is stunning. GO FOR CLARITY first.

So talking thru the code below: (1) process only things that are US dollars (optional), (2) there is gonna be zero or more spaces, followed by the "$" sign and some sequence of digits,commas and periods. (3)that sequence gets captured and then the commas get deleted.

Update: If max valid dollar amount is $100,000.00 then I would enforce the 100,000 max via a numeric comparison on $number. BUT, this is actually the wrong place in the system to be checking that!

#!/usr/bin/perl -w use strict; my @bucks = ('$1,999,003 USD ', '$1,532.33 AUS', '$1,500.11 USD'); foreach my $amount (@bucks) { next unless $amount =~ m/USD/; my ($comma_amount) = ($amount =~ /\s*\$([\d,.]+)/); print "c=$comma_amount\n"; (my $number = $comma_amount) =~ s/,//g; print "n=$number\n"; } __END__ prints: c=1,999,003 n=1999003 c=1,500.11 n=1500.11

In reply to Re: matching USD amounts by Marshall
in thread matching USD amounts by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.