On Nov 23, 9:22 am, jwkr...@shaw.ca (John W. Krahn) wrote: > 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
I just wanted to thank everyone who replied all of them worked thanks for the help. John thanks for all of the diffrent wanys to do this also. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/