$foo = "My number is 123456"; $num = $foo; $num =~ s/(\W|\D)//g; print $num;