简体   繁体   English

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

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

I am writing some code and I need to do the following: 我正在编写一些代码,我需要执行以下操作:

Given a 4 digit number like "1234" I need to get 3 pairs of numbers (the first 2, the 2 in the middle, and the last 2), in this example I need to get "12" "23" and "34". 给定一个4位数字,例如“ 1234”,我需要获得3对数字(前2个,中间2个,最后2个),在此示例中,我需要获得“ 12”,“ 23”和“ 34” ”。

I am new to perl and don't know anything about regex. 我是perl的新手,对regex一无所知。 In fact, I am writing a script for personal use and I've started reading about Perl some days ago because I figured it was going to be a better language for the task at hand (need to do some statistics with the numbers and find patterns) 实际上,我正在编写一个供个人使用的脚本,几天前我开始阅读有关Perl的信息,因为我认为这将是一种更好的语言,可以处理当前的任务(需要对数字进行一些统计并查找模式) )

I have the following code but when testing I processed 6 digit numbers, because I "forgot" that the numbers I would be processing are 4 digits, so it failed with the real data, of course 我有以下代码,但是在测试时我处理了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++;
    }
}

Thank you. 谢谢。

You can move last matching position with pos() 您可以使用pos()移动最后一个匹配位置

pos directly accesses the location used by the regexp engine to store the offset, so assigning to pos will change that offset.. pos直接访问正则表达式引擎用于存储偏移量的位置,因此分配给pos将更改该偏移量。

my $item = 1234;

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

The simplest way would be to use a global regex pattern search 最简单的方法是使用全局正则表达式模式搜索

It is nearly always best to separate verificaton of the input data from processing , so the program below first rejects any values that are not four characters long or that contain a non-digit character 几乎总是最好将输入数据的验证处理分开,因此下面的程序首先拒绝所有长度不超过四个字符或包含非数字字符的值

Then the regex pattern finds all points in the string that are followed by two digits, and captures them 然后,正则表达式模式会找到字符串中所有后跟两位数字的点,并捕获它们

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

output 输出

12 23 34
65 57 72

Here's a pretty loud example demonstrating how you can use substr() to fetch out the portions of the number, while ensuring that what you're dealing with is in fact exactly a four-digit number. 这是一个非常响亮的示例,展示了如何使用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

Output: 输出:

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