use strict; use warnings; my $string = '1potato 2potato 3potato 4 potato'; print +( $string =~ m/(\d+potato)/g )[2];