in reply to matching USD amounts
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
|
|---|