Extract the required substring from another string -Perl

后端 未结 4 788
轮回少年
轮回少年 2020-12-19 16:45

I want to extract a substring from a line in Perl. Let me explain giving an example:

fhjgfghjk3456mm   735373653736
icasd 666666666666
111111111111
<         


        
4条回答
  •  一生所求
    2020-12-19 17:28

    The $1 built-in variable stores the last match from a regex. Also, if you perform a regex on a whole string, it will return the whole string. The best solution here is to put parentheses around your match then print $1.

    my $strn = "fhjgfghjk3456mm 735373653736\nicasd\n666666666666 111111111111";
    $strn =~ m/([0-9]{12})/;
    print $1;
    

    This makes our regex match JUST the twelve digit number and then we return that match with $1.

提交回复
热议问题