我想从 Perl 中的一行中提取一个子字符串。让我举例说明:
fhjgfghjk3456mm 735373653736
icasd 666666666666
111111111111
在以上几行中,我只想提取 12 位数字。我尝试使用split
功能:
my @cc = split(/[0-9]{12}/,$line);
print @cc;
但它所做的是删除字符串的匹配部分并将残差存储在@cc
. 我希望打印与图案匹配的部分。我该怎么做?
您可以使用正则表达式来做到这一点:
#!/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
----------------------------------------------------------------------
#!/bin/perl
my $var = 'fhjgfghjk3456mm 735373653736 icasd 666666666666 111111111111';
if($var =~ m/(\d{12})/) {
print "Twelve digits: $1.";
}
$1 内置变量存储来自正则表达式的最后一个匹配项。此外,如果您对整个字符串执行正则表达式,它将返回整个字符串。最好的解决方案是在匹配项周围加上括号,然后打印 $1。
my $strn = "fhjgfghjk3456mm 735373653736\nicasd\n666666666666 111111111111";
$strn =~ m/([0-9]{12})/;
print $1;
这使得我们的正则表达式只匹配 12 位数字,然后我们返回匹配的 $1。
#!/usr/bin/env perl
undef $/;
$text = <DATA>;
@res = $text =~ /\b\d{12}\b/g;
print "@res\n";
__DATA__
fhjgfghjk3456mm 735373653736
icasd 666666666666
111111111111