Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Numbers only in a string

by powerhouse (Friar)
on Sep 08, 2005 at 15:39 UTC ( #490243=perlquestion: print w/replies, xml ) Need Help??

powerhouse has asked for the wisdom of the Perl Monks concerning the following question:

I need to get the users input for tech support, I have a dialer that will call them back for the support, but the phone number can only be ten digits long. So first I want to get ONLY the numbers, then I can play with it, like if it is 11 digits in length, and the first digit is a 1 then I will have it drop off the one. My problem is that I cannot get it to work, it does not strip off anything.

Does this not work:
$phone =~ s/!(^\d+)//g;

I've searched a few sites, including this one, usually for the terms, "phone number only", "numbers only" "strip everything but numbers" and so forth, but found nothing.

Replies are listed 'Best First'.
Re: Numbers only in a string
by sk (Curate) on Sep 08, 2005 at 15:51 UTC
    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
      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
Re: Numbers only in a string
by davido (Cardinal) on Sep 08, 2005 at 15:51 UTC

    You're sort of on the right track...


    That will get the RE to do what you're trying to do.


Re: Numbers only in a string
by socketdave (Curate) on Sep 08, 2005 at 16:01 UTC
    $phone =~ s/\D|^1//g;

    Same as davido and sk, but also strips a leading 1 from the number.

Re: Numbers only in a string
by saberworks (Curate) on Sep 08, 2005 at 16:01 UTC

    \D is the opposite of \d so it will replace anything that's not a digit.
      don't need to use a character class here -- just:
Re: Numbers only in a string
by trammell (Priest) on Sep 08, 2005 at 16:26 UTC
    my $s = '1-(800)-223-5555'; (my $n = $s) =~ y/0-9//cd;
Benchmark time: Numbers only in a string
by polettix (Vicar) on Sep 08, 2005 at 17:26 UTC
    I was just curious about it, so here you go:
    #!/usr/bin/perl use strict; use warnings; use Benchmark qw( cmpthese ); my @data = split /\n/, <<END; 800-800-1212 (800)800-1212 (800) 800-1212 800 800 1212 END sub dosloop { my $re = shift; (my $phone = $_) =~ s/$re//g foreach @data; } sub doyloop { my $fake = shift; (my $phone = $_) =~ y/0-9//cd foreach @data; } cmpthese(-10, { single => sub { dosloop(qr/\D/) }, multiple=> sub { dosloop(qr/\D+/) }, y => sub { doyloop(qr/\D/) }, # Just a fake parameter } );
    I tried to be as fair as possible and have the same overhead for all calls. The results are the following:
    Rate multiple single y multiple 95932/s -- -6% -39% single 101881/s 6% -- -35% y 156303/s 63% 53% --
    The simple translation outperforms the other, as expected. I was particularly curious about putting the '+' or not. I vaguely remember some discussion about it before, but I couldn't tell you where.

    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Numbers only in a string
by sh1tn (Priest) on Sep 08, 2005 at 16:34 UTC
    while(<SOME_INPUT>){ $digits_only .= (join '', split /\D+/) . $/ }

Re: Numbers only in a string
by Roy Johnson (Monsignor) on Sep 09, 2005 at 12:06 UTC
    Another job for tr:
    $phone =~ tr/0-9//dc;
    [oops, didn't see trammell's reply when I posted this redundant one]

    Caution: Contents may have been coded under pressure.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://490243]
Approved by holli
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2021-04-19 16:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found