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

I have records where I just need the number and need a reg expression to get the number:

Person_0011
Person_0012
Person_0013
Person_0014

My attempt didnt work:
s/\w(6)\_(\d(4))/$1/;
I know it looks bad which is why i need help on it.

Replies are listed 'Best First'.
Re: Fetch numbers only
by ikegami (Patriarch) on Feb 14, 2006 at 19:20 UTC
    You're using parens where you should be using curlies. (And '_' doesn't need escaping.)
    s/\w{6}_(\d{4})/$1/;
    which is pretty much the same as the following
    s/\w{6}_//;
    The following should be faster, though:
    $_ = substr($_, -4);
Re: Fetch numbers only
by smokemachine (Hermit) on Feb 14, 2006 at 19:20 UTC
    $num=$1 if$var=~/(\d+)$/
      Thanks for the very quick responses and answering my question.
Re: Fetch numbers only
by explorer (Chaplain) on Feb 14, 2006 at 19:16 UTC
    (my $number) = $text =~ /(\d+)/;
Re: Fetch numbers only
by Praveen (Friar) on Feb 15, 2006 at 05:12 UTC
    s/(.*?)\_(\d+)/$2/;