我有以下正则表达式来匹配 readelf -Ws
的输出:
my $regex = qr{ ^\s+(?'Num'\d+):
\s+(?'Value'\w+)
\s+(?'Size'(?:\d+|0x[0-9a-fA-f]+))
\s+(?'Type'\w+)
\s+(?'Bind'\w+)
\s+(?'Vis'\w+)
\s+(?'Ndx'\w+)
\s+(?'Name'\S+)
}x;
...虽然它可能并不完美,但它足以满足我的需求。
理想情况下,其使用方式是:
while( <> ) {
chomp;
m{${regex}} || next;
# an implicit assertion here is that length($+{Name}) > 0
if( $+{Type} =~ m{something}
&& $+{Bind} =~ m{something}
...
...但是,%+
在第一个正则表达式之后被破坏。我不知道如何复制 %+
背后的哈希值。有可能吗?如果可以的话我该怎么做?
显然可以执行以下操作:
while( <> ) {
chomp;
my ($Num, $Value, $Size, $Type, $Bind, $Vis, $Ndx, $Name) = ($_ =~ m{${regex}});
next unless defined( $Name );
if( $Type =~ m{something}
&& $Bind =~ m{something}
...
...但我更喜欢使用命名子表达式,因为它可以帮助使正则表达式自记录。
最佳答案
%captures = %+;
use Data::Dumper qw( Dumper );
local $_ = 'abc123';
my @captures;
while (/(?'Letters'\pL+)|(?'Digits'\pN+)/g) {
my %captures = %+;
push @captures, \%captures;
}
print(Dumper(\@captures));
$VAR1 = [
{
'Letters' => 'abc'
},
{
'Digits' => '123'
}
];
或者由于仅存在定义的字段,因此您可以使用
%captures = ( %captures, %+ );
或
$captures{$_} = $+{$_} for keys %+;
use Data::Dumper qw( Dumper );
local $_ = 'abc123';
my %captures;
while (/(?'Letters'\pL+)|(?'Digits'\pN+)/g) {
%captures = ( %captures, %+ );
}
print(Dumper(\%captures));
$VAR1 = {
'Letters' => 'abc',
'Digits' => '123'
};
关于regex - 使用命名子表达式时是否可以复制 %+ 哈希?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26935972/