2

我有一个格式为:

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。/(\d+\.){3}\d+/和以 4 开头的 10 位数字,例如。/[4][0-9]{9}/但是如何从同一个字符串一起打印它们?

4

2 回答 2

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;
于 2012-08-08T08:03:16.290 回答
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]
于 2012-08-08T08:05:06.700 回答