小编Mat*_*lis的帖子

获取所有 perl 正则表达式捕获组的值

问题:我正在编写一个库,它接收用户提供的正则表达式,其中包含要针对其他输入运行的未知数量的捕获组,并且我想提取连接在一个字符串中的所有捕获组的值(以便在其他地方进一步处理)。

如果预先知道捕获组的数量,这是微不足道的,因为我只是指定它们:

#!/usr/bin/perl -w
my $input = `seq -s" " 100 200`;
my $user_regex = 
 qr/100(.*)103(.*)107(.*)109(.*)111(.*)113(.*)116(.*)120(.*)133(.*)140(.*)145/;

if ($input =~ $user_regex)  { print "$1 $2 $3 $4 $5 $6 $7 $8 $9 $10\n"; }
Run Code Online (Sandbox Code Playgroud)

正确产生(忽略额外的空格):

 101 102   104 105 106   108   110   112   114 115   117 118 119 
 121 122 123 124 125 126 127 128 129 130 131 132   
 134 135 136 137 138 139   141 142 143 144
Run Code Online (Sandbox Code Playgroud)

但是,如果有 10 个以上的捕获组,如果我不修改代码,我就会丢失数据。由于捕获组的数量未知,目前我在no warningspragma下使用了数百个手动指定的匹配(“$1”到“$200”)并希望它足够了,但它似乎并不特别干净健壮。

理想情况下,我想要一些像values %+ …

regex perl

3
推荐指数
1
解决办法
2241
查看次数

使用直方图确定有色物体的存在?

我试图确定图片的一部分是否包含红白条纹对象(liftramp).如果它存在,它看起来像这样:当下,当不是这样的时候: 没有

天真的方法是提取直方图,并计算红色像素是否多于蓝色/绿色像素:

use Image::Magick;

my $image = Image::Magick->new;
my $rv = $image->Read($picture);
#$rv = $image->Crop(geometry=>'26x100+484+40');

my @hist_data = $image->Histogram;
my @hist_entries;

# Histogram returns data as a single list, but the list is actually groups of 5 elements. Turn it into a list of useful hashes.
while (@hist_data) {
    my ($r, $g, $b, $a, $count) = splice @hist_data, 0, 5;
    push @hist_entries, { r => $r, g => $g, b => $b, alpha => $a, count => $count }; …
Run Code Online (Sandbox Code Playgroud)

perl comparison imagemagick histogram

2
推荐指数
2
解决办法
926
查看次数

标签 统计

perl ×2

comparison ×1

histogram ×1

imagemagick ×1

regex ×1