繁体   English   中英

Perl:匹配4对连续数字中的3对数字

[英]Perl: Matching 3 pairs of numbers from 4 consecutive numbers

我正在编写一些代码,我需要执行以下操作:

给定一个4位数字,例如“ 1234”,我需要获得3对数字(前2个,中间2个,最后2个),在此示例中,我需要获得“ 12”,“ 23”和“ 34” ”。

我是perl的新手,对regex一无所知。 实际上,我正在编写一个供个人使用的脚本,几天前我开始阅读有关Perl的信息,因为我认为这将是一种更好的语言,可以处理当前的任务(需要对数字进行一些统计并查找模式) )

我有以下代码,但是在测试时我处理了6位数字,因为我“忘记”了我将要处理的数字是4位数字,所以它在实际数据中当然失败了

foreach $item (@totaldata)
{
    my $match;

    $match = ($item =~ m/(\d\d)(\d\d)(\d\d)/);

    if ($match) 
    { 
    ($arr1[$i], $arr2[$i], $arr3[$i]) = ($item =~ m/(\d\d)(\d\d)(\d\d)/);
    $processednums++; 
    $i++;
    }
}

谢谢。

您可以使用pos()移动最后一个匹配位置

pos直接访问正则表达式引擎用于存储偏移量的位置,因此分配给pos将更改该偏移量。

my $item = 1234;

my @arr;
while ($item =~ /(\d\d)/g) {
  push @arr, $1;
  pos($item)--;
}
print "@arr\n"; # 12 23 34

最简单的方法是使用全局正则表达式模式搜索

几乎总是最好将输入数据的验证处理分开,因此下面的程序首先拒绝所有长度不超过四个字符或包含非数字字符的值

然后,正则表达式模式会找到字符串中所有后跟两位数字的点,并捕获它们

use strict;
use warnings 'all';

for my $val ( qw/ 1234 6572 / ) {

    next if length($val) != 4 or $val =~ /\D/;

    my @pairs = $val =~ /(?=(\d\d))/g;
    print "@pairs\n";
}

输出

12 23 34
65 57 72

这是一个非常响亮的示例,展示了如何使用substr()提取数字的各个部分,同时确保您要处理的实际上是一个四位数的数字。

use warnings;
use strict;

my ($one, $two, $three);

while (my $item = <DATA>){
    if ($item =~ /^\d{4}$/){
        $one   = substr $item, 0, 2;
        $two   = substr $item, 1, 2;
        $three = substr $item, 2, 2;
        print "one: $one, two: $two, three: $three\n";
    }
}

__DATA__
1234
abcd
a1b2c3
4567
891011

输出:

one: 12, two: 23, three: 34
one: 45, two: 56, three: 67
foreach $item (@totaldata) {
    if ( my @match = $item =~ m/(?=(\d\d))/ ) {
        ($heads[$i], $middles[$i], $tails[$i]) = @match;
        $processednums++; 
        $i++;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM