But I think substr is better .

my $str="0000000000000111";
my $r=substr ($str,-3);

> -----邮件原件-----
> 发件人: John W. Krahn [mailto:jwkr...@shaw.ca]
> 发送时间: 2009年11月日 23:23
> 收件人: Perl Beginners
> 主题: Re: Regex to get last 3 digits of a number.
> 
> shadow52 wrote:
> > Hey everyone,
> 
> Hello,
> 
> > I am trying to get just the last 3 numbers from the following number
> > from perl using regexs but I have not had no success so I was hoping
> > that I could get a little help on this. I just ordered the regex book
> > from oreilly so that hopefully in the future I will not have to ask a
> > question like this again.
> >
> > The number is 0000000000000111
> >
> > I was just wanting to get the last 3 digits from this number to be
> > able to get an exact word phrase from my already loaded Hash table
> > that I have created for various numbers that I will get the last 3
> > numbers from.
> 
> To get the last three characters:
> 
> $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
> /(...)$/; print $last_3'
> 111
> 
> Or:
> 
> $ perl -le'my $number = "0000000000000111"; my $last_3 = substr $number,
> -3; print $last_3'
> 111
> 
> Of course the regex version will ignore any newline at the end.
> 
> To get the last three numerical digits only:
> 
> $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
> /(\d\d\d)$/; print $last_3'
> 111
> 
> Or:
> 
> $ perl -le'my $number = "0000000000000111"; my ( $last_3 ) = $number =~
> /.*(\d\d\d)/; print $last_3'
> 111
> 
> 
> \d\d\d could also be written as \d{3}.
> 
> 
> > Oh yeah all I was doing was putting this number in a
> > file and then passing it to perl on the command line incase that is
> > needed.
> 
> I can not think of why that may be needed?
> 
> 
> 
> John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity.               -- Damian Conway
> 
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to