使用量词时填充自动 Perl 变量

发布于 2024-11-05 06:04:58 字数 384 浏览 4 评论 0原文

我试图将以下行

      5474c2ef012a759a c11ab88ae8daa276 63693b53799c91f1 be1d8c8738733d80 

  if(/[[:xdigit:]{8}[:xdigit:]{8}\s]{4}/)

是否有无论如何我用每个单词的一半填充自动变量 $1,$2,$3..$8 等相匹配。 IE

  $1=5474c2ef
  $2=012a759a
  $3=c11ab88a
  $4=e8daa276
  $5=63693b53
  $6=799c91f1
  $7=be1d8c87
  $8=38733d80

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如果没有 2024-11-12 06:04:58

您可以将它们捕获在一个数组中:(

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);
}

如果有四个以上或者如果有较早的 16 进制数字序列,并且不仅仅由一个空格分隔,则其行为可能与原始序列不同)。

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).

许久 2024-11-12 06:04:58

怎么样:

my $pat = '([[:xdigit:]]{8})\s?' x 8;
# produces: ([[:xdigit:]]{8})\s?([[:xdigit:]]{8})\s?....
/$pat/;

如果您需要严格遵守间距要求,请更新

my $pat = join('\s', map{'([[:xdigit:]]{8})' x 2} (1..4));
# produces: ([[:xdigit:]]{8})([[:xdigit:]]{8})\s....
/$pat/;

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/;
初心未许 2024-11-12 06:04:58
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'
        ];
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'
        ];
野侃 2024-11-12 06:04:58

是的,有,但你不想。

你只想这样做:

 while ( /(\p{ahex}{8})/g ) { print "got $1\n" }

Yes, there is, but you don’t want to.

You just want to do this:

 while ( /(\p{ahex}{8})/g ) { print "got $1\n" }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文