$foo = '1 a 2 b 3 c'; while($foo =~ s/(\d)[\D]*$//) { print $1; } __output__ 321