从另一个字符串-Perl中提取所需的子字符串

Ame*_*mey 4 string perl grep split

我想从Perl中的一行中提取子字符串.让我解释一下这个例子:

fhjgfghjk3456mm   735373653736
icasd 666666666666
111111111111
Run Code Online (Sandbox Code Playgroud)

在上面的行中,我只想提取12位数字.我尝试使用split功能:

my @cc = split(/[0-9]{12}/,$line);
print @cc;
Run Code Online (Sandbox Code Playgroud)

但它的作用是移除字符串的匹配部分并将残留物存储在其中@cc.我想要打印匹配模式的部分.我怎么样?

sim*_*que 8

你可以使用正则表达式:

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

在这里测试正则表达式: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
----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)