in reply to pricing and phone number regexes

These are untested, and still have limitations, on which I'll elaborate in a moment:

The limitations: With respect to phone numbers, if the number is something like 1-800-GO-FISH you're out of luck. Also, it has to be a USA number of seven or ten, or eleven (including the leading 1) digits.

With respect to the pricing, you have to have something in front of the optional decimal place, and you have to have a dollar sign. Otherwise it will fail to match. Oh, and if the price is "One Million Dollars!" you won't capure it, since it's not numeric.

There are probably other limitations as well.

You should also consider looking at Number::Phone::US. Here's a snippet:

use Number::Phone::US qw/is_valid_number/; print "Phone: $number\n" if is_valid_number( $number );

Also, be sure to have a look at Regexp::Common::number. You might be able to either make use of it, or glean some knowledge from its source code.

UPDATE: I've had time to test and rework them a little now. I fixed the missing ')' in the phone number RE, and added some logic to invalidate prices that have more than two digits to the right of the decimal point.

PS: I recommend passing my RE's through YAPE::Regex::Explain so that you can see a detailed description of what they do. It's pretty easy to use.


Dave

Replies are listed 'Best First'.
Re^2: pricing and phone number regexes
by coldfingertips (Pilgrim) on Jun 05, 2004 at 03:31 UTC
    I can't seem to get any of those to work, they keep producing use uninitialized errors when I try to print the results. I know you said it was unterested but can you tell me where the missing ) is supposed to go in the phone regex? I've tried a few spots but I can't figure out where it's supposed to be.

    Thanks!

      I just did some checking and testing (finally had a moment to do it). I've fixed both RE's. Actually the second one worked all along, but had a problem with allowing prices with more than two decimal places. That's fixed. And the phone number RE was missing a close paren.

      Anyway, it's sorted out now, and the versions now appearing in my original followup are correct, still with the limitations I mentioned before.


      Dave

        Thanks again for the regexes but I can't seem to get them to work..either of them. I must be doing something wrong, can you or anyone else take a quick look to see what it might be? It's still use of uninitialized value errors.
        $/="====\n\n"; open (READFROM, "$readfrom") or die "Cannot open $readfrom: $!"; open (WRITETO, ">$writeto") or die "Cannot open $writeto: $!"; while ( <READFROM> ) { chomp; $_ =~ m/\$ (?:\d{1,3},?)+ (?:\.\d{0,2})? (?![.\d]) /x; my $price = "$2"; print "$price\n"; } close (WRITETO) or die "Cannot close $writeto: $!"; close (READFROM) or die "Cannot close $readfrom: $!";