【问题标题】:Extract the required substring from another string -Perl从另一个字符串中提取所需的子字符串 -Perl
【发布时间】:2012-07-02 21:26:39
【问题描述】:

我想从 Perl 中的一行中提取一个子字符串。让我举例说明:

fhjgfghjk3456mm   735373653736
icasd 666666666666
111111111111

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

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

但它所做的是删除字符串的匹配部分并将残差存储在@cc 中。我希望打印与图案匹配的部分。我该怎么做?

【问题讨论】:

    标签: string perl split grep


    【解决方案1】:

    您可以使用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
    ----------------------------------------------------------------------
    

    【讨论】:

      【解决方案2】:

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

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

      这使得我们的正则表达式只匹配 12 位数字,然后我们返回匹配的 $1。

      【讨论】:

      • 谢谢.. 这很简单.. 但相信我,在发布这个问题之前我读了很多。非常感谢..
      • 你不应该在没有条件的情况下使用$1
      【解决方案3】:
      #!/bin/perl
      my $var = 'fhjgfghjk3456mm 735373653736 icasd 666666666666 111111111111';
      if($var =~ m/(\d{12})/) {
        print "Twelve digits: $1.";
      }
      

      【讨论】:

        【解决方案4】:
        #!/usr/bin/env perl
        
        undef $/;
        $text = <DATA>;
        @res = $text =~ /\b\d{12}\b/g;
        print "@res\n";
        
        __DATA__
        fhjgfghjk3456mm   735373653736
        icasd 666666666666
        111111111111
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-29
          • 1970-01-01
          • 2022-06-21
          • 1970-01-01
          • 2011-07-21
          • 1970-01-01
          • 2021-01-09
          相关资源
          最近更新 更多