Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

print last 3 numeric numbers from a string

Status
Not open for further replies.

curmmie

MIS
Jun 23, 2003
1
US
New at Perl, still grap regex stuff. I have a string " abcdefghi 22 33 44" and i want to print out the last 3 numeric valuse of the string. Below is what im trying, i get no output. Thanks in advance for any info.

$string="tttt 22 22 22";
$string=~ s/^.*:\s(\d{2}),\s(\d{2}),\s(\d{2})//;
print "$1 $2 $3";
 
try this instead:

$string="tttt 22 22 22";
$string=~ m/.+(\d{2}).+(\d{2}).+(\d{2})/;
print "$1 $2 $3"

-MikeyG
 
I'd change the regex to this:

$string=~ m/(\d+)\D+(\d+)\D+(\d+)\D*$/;

Anchoring the regex to the end of the string, only matching for non-digits (\D) between the digits, and are you sure that you'll only get 2 digits in each number?

Added the \D* just in case there might be trailing whitespace of dodgy characters. Ignore if you know you don't need it.


Barbie
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top