Test anxiety
--#--
Before we spot the flaw, let's do a little trick. Let's build a test
harness for our regex. This is a sensible thing to do whenever
we're trying to build a regular expression matcher.
#!/usr/bin/perl
open(FH, $ARGV[0]);
@strings = ;
close FH;
foreach $string_to_test(@strings) {
chomp($string_to_test);
if ($string_to_test =~ m/^\d{0,3},?(\d{3},?)*\d{3}(\.\d{2})?$/) {
print "$string_to_test: PASSED\n";
}
else {print "$string_to_test: FAILED\n";}
}
Using this script, we can feed our regex candidate a list of strings.
This way, we can systematically alter the patterns we put in the
string, copy and paste, etc. In a few seconds you can build a list to
test your regex in various ways, and do it systematically rather
than randomly guessing what will make it pass or fail.
Here's another trick: Put all the strings you think will pass
into one list and file, and the ones you think will fail in another. That
makes it easier to spot the exceptions that prove you wrong.
So, what's the flaw?