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

时间:2012-07-02 12:54:43

标签: string perl split grep

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

fhjgfghjk3456mm   735373653736
icasd 666666666666
111111111111

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

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

但它的作用是删除字符串的匹配部分并将残差存储在@cc中。我想要打印匹配模式的部分。我怎么做?

4 个答案:

答案 0 :(得分:8)

您可以使用regular expressions

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

在此处测试正则表达式: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
----------------------------------------------------------------------

答案 1 :(得分:3)

#!/bin/perl
my $var = 'fhjgfghjk3456mm 735373653736 icasd 666666666666 111111111111';
if($var =~ m/(\d{12})/) {
  print "Twelve digits: $1.";
}

答案 2 :(得分:3)

$ 1内置变量存储正则表达式的最后一个匹配。此外,如果对整个字符串执行正则表达式,它将返回整个字符串。这里最好的解决方案是在比赛中加上括号,然后打印$ 1.

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

这使得我们的正则表达式只匹配十二位数字,然后我们将该匹配返回给$ 1.

答案 3 :(得分:1)

#!/usr/bin/env perl

undef $/;
$text = <DATA>;
@res = $text =~ /\b\d{12}\b/g;
print "@res\n";

__DATA__
fhjgfghjk3456mm   735373653736
icasd 666666666666
111111111111