in reply to Numbers only in a string

So close :)

#!/usr/bin/perl -w while(<DATA>) { s/[^\d]+//g; print $_,$/; } __DATA__ 800-800-1212 (800)800-1212 (800) 800-1212 800 800 1212

__END__ 8008001212 8008001212 8008001212 8008001212

Replies are listed 'Best First'.
Re^2: Numbers only in a string
by superfrink (Curate) on Sep 09, 2005 at 05:02 UTC
    I added one line to get up to 10 digits starting from the right side. This will remove the leading "1" and I think any country code. (I'm assuming the phone number is in the format for Canada/USA.)
    while(<DATA>) { s/[^\d]+//g; # remove non-digit characters # only digit characters remain m/(.{0,10})$/; # find as many as 10 characters, # starting at the right print $1,$/; # print the found digits } __DATA__ 800-1212 1-800-800-1212 1(800)800-1212 1 (800) 800-1212 1 800 800 1212