2012-07-02 41 views
3

我想从Perl中的一行中提取子字符串。让我解释一个例子:从另一个字符串中提取所需的子字符串-Perl

fhjgfghjk3456mm 735373653736 
icasd 666666666666 
111111111111 

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

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

但它的作用是去除字符串匹配的部分,并存储在@cc残渣。我想要打印与图案匹配的部分。我怎么样?

回答

3

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

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

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

+0

感谢..这是非常简单的。但请相信我,我发布这个问题之前,读了很多。非常感谢.. – Amey

+7

如果没有条件,你不应该使用'$ 1'。 – TLP

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

... Rawkode上解决了我的问题.. – Amey

7

你可以用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
#!/usr/bin/env perl 

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

__DATA__ 
fhjgfghjk3456mm 735373653736 
icasd 666666666666 
111111111111 
感谢
相关问题