在Perl中以相同的字符串打印多个reg expr匹配项

时间:2012-08-08 07:45:51

标签: regex perl

我有一个格式为的文件:

20120807 175041.438  5976.022 E   27000 [PRE:4712345678: Just some text HERE '127.0.0.1' or APU_ID '' - DEFAULTING TO WORLD_PLAN_9 ZONE]
20120807 175041.438  5976.022 E   27000 [PRE:4722345679: Just some text HERE '127.0.0.2' or APU_ID '26002' - DEFAULTING TO WORLD_PLAN_9 ZONE]
..

我想要提取的是:

20120807;4712345678;127.0.0.1;;
20120807;4722345679;127.0.0.2;26002;

我知道我可以使用例如提取IP。使用例如{4}以及以4开头的10位数字。 /(\d+\.){3}\d+/但是如何从同一个字符串中一起打印它们?

2 个答案:

答案 0 :(得分:1)

while (<DATA>) {
    @ds = /^(\d+).*?PRE:(\d+):[^']+'([^']+)' or APU_ID '(\d*)'/;
    print "$_;" for @ds;
    print "\n";
}

__DATA__
20120807 175041.438 5976.022 E 27000 [PRE:4712345678: Just some text HERE '127.0.0.1' or APU_ID '' - DEFAULTING TO WORLD_PLAN_9 ZONE]
20120807 175041.438 5976.022 E 27000 [PRE:4722345679: Just some text HERE '127.0.0.2' or APU_ID '26002' - DEFAULTING TO WORLD_PLAN_9 ZONE]

输出:

20120807;4712345678;127.0.0.1;;
20120807;4722345679;127.0.0.2;26002;

答案 1 :(得分:0)

use strict;

while(my $line = <DATA>) {

    $line =~ m{
            ^
            (\d+)              # first number
            .*?
            (\d{10})           # 10 digits number
            .*? 
            ((?:\d+\.){3}\d+)  # ip
            .*?
            APU_ID\s' 
            (\d*)              # apu number
            '
    }x;

    printf "%s %s %s %s\n", $1, $2, $3, $4;
}

__DATA__
20120807 175041.438  5976.022 E   27000 [PRE:4712345678: Just some text HERE '127.0.0.1'     or APU_ID '' - DEFAULTING TO WORLD_PLAN_9 ZONE]
20120807 175041.438  5976.022 E   27000 [PRE:4722345679: Just some text HERE '127.0.0.2' or  APU_ID '26002' - DEFAULTING TO WORLD_PLAN_9 ZONE]