regexperlawk

Return values for string matching


Pretend I have a code in awk:

str_1 = "abc123defg";
match(str_1, /[0-9]+/);
num_1 = substr(str_1, RSTART, RLENGTH);

Then num_1 will be "123". What is the Perl version of the same task?


Solution

  • I'd translate that to:

    my $num_1 = ($str_1 =~ /(\d+)/)[0];