m/ (?: (?:1-?)? (?:\(?[0-9]{3}\)?-)? )? [0-9]{3}-?[0-9]{4} /x
m/ \$ (?:\d{1,3},?)+ (?:\.\d{0,2})? (?![.\d]) /x
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
In reply to Re: pricing and phone number regexes
by davido
in thread pricing and phone number regexes
by coldfingertips
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |