Populating Automatic Perl Variables when using Quantifiers
I was trying to match the following line
5474c2ef012a759a c11ab88ae8daa276 63693b53799c91f1 be1d8c8738733d80
with
if(/[[:xdigit:]{8}[:xdigit:]{8}\s]{4}/)
Is there anyway I populate the automatic variables $1,$2,$3..$8 etc with half of 开发者_如何学运维each of those words. i.e
$1=5474c2ef
$2=012a759a
$3=c11ab88a
$4=e8daa276
$5=63693b53
$6=799c91f1
$7=be1d8c87
$8=38733d80
You could capture them in an array:
use strict;
use warnings;
use Data::Dumper;
$_ = '5474c2ef012a759a c11ab88ae8daa276 63693b53799c91f1 be1d8c8738733d80 ';
my @nums = /\G(?:([[:xdigit:]]{8})([[:xdigit:]]{8})\s)/g;
if (@nums >= 8) {
print Dumper(\@nums);
}
(may behave differently than the original if there are more than four or if there're earlier 16-hex-digit sequences separated by more than just a space).
How about:
my $pat = '([[:xdigit:]]{8})\s?' x 8;
# produces: ([[:xdigit:]]{8})\s?([[:xdigit:]]{8})\s?....
/$pat/;
Update if you need to be strict on the spacing requirement:
my $pat = join('\s', map{'([[:xdigit:]]{8})' x 2} (1..4));
# produces: ([[:xdigit:]]{8})([[:xdigit:]]{8})\s....
/$pat/;
use strict;
use warnings;
use Data::Dumper;
$_ = '5474c2ef012a759a c11ab88ae8daa276 63693b53799c91f1 be1d8c8738733d80 ';
if (/((?:[[:xdigit:]]{16}\s){4})/) {
my @nums = map { /(.{8})(.{8})/ } split /\s/, $1;
print Dumper(\@nums);
}
__END__
$VAR1 = [
'5474c2ef',
'012a759a',
'c11ab88a',
'e8daa276',
'63693b53',
'799c91f1',
'be1d8c87',
'38733d80'
];
Yes, there is, but you don’t want to.
You just want to do this:
while ( /(\p{ahex}{8})/g ) { print "got $1\n" }
精彩评论