How to extract a number from a string in Perl?

perl

Solution

$str =~ s/\D//g;

This removes all nondigit characters from the string. That's all that you need to do.

EDIT: if Unicode digits in other scripts may be present, a better solution is:

$str =~ s/[^0-9]//g;

Problem

I have ``` print $str; abcd*%1234$sdfsd..#d ``` The string would always have only one continuous stretch of numbers, like `1234` in this case. Rest all will be either alphabets or other special characters. How can I extract the number (`1234` in this case) and store it back in `str`? This page suggests that I should use `\d`, but how?

Original source