Extract the required substring from another string -Perl

后端 未结 4 790
轮回少年
轮回少年 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:26

    You can do it with regular expressions:

    #!/usr/bin/perl
    my $string = 'fhjgfghjk3456mm 735373653736 icasd 666666666666 111111111111';
    while ($string =~ m/\b(\d{12})\b/g) {
      say $1;
    }
    

    Test the regex here: http://rubular.com/r/Puupx0zR9w

    use YAPE::Regex::Explain;
    print YAPE::Regex::Explain->new(qr/\b(\d+)\b/)->explain();
    
    The regular expression:
    
    (?-imsx:\b(\d+)\b)
    
    matches as follows:
    
    NODE                     EXPLANATION
    ----------------------------------------------------------------------
    (?-imsx:                 group, but do not capture (case-sensitive)
                             (with ^ and $ matching normally) (with . not
                             matching \n) (matching whitespace and #
                             normally):
    ----------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    ----------------------------------------------------------------------
      (                        group and capture to \1:
    ----------------------------------------------------------------------
        \d+                      digits (0-9) (1 or more times (matching
                                 the most amount possible))
    ----------------------------------------------------------------------
      )                        end of \1
    ----------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    ----------------------------------------------------------------------
    )                        end of grouping
    ----------------------------------------------------------------------
    

提交回复
热议问题