Perl Regex - 打印匹配的条件正则表达式

时间:2017-07-26 16:40:20

标签: regex perl conditional

我正在尝试从日志文件中提取一些模式,但我无法正确打印它们。

日志字符串示例:

1) sequence_history/buckets/FPJ.INV_DOM_16_PRD.47269.2644?startid=2644000&endid=2644666

2) sequence_history/buckets/FPJ.INV_DOM_16_PRD.41987.9616

我想提取3件事:

  

A =“FPJ.INV_DOM_16_PRD”B =“47269”C = 9616或2644666(如果该行   已结束然后C = 2644666否则它是9616)

日志行可以是类型1或2.我能够提取A和B,但我被C语言困住,因为我需要一个条件语句,我无法正确提取它。我正在粘贴我的代码:

my $string='/sequence_history/buckets/FPJ.INV_DOM_16_PRD.47269.2644?startid=2644000&endid=2644666';

if ($string =~ /sequence_history\/buckets\/(.*)/){
    my $line = $1;
    print "$line\n";
    if($line =~ /(FPJ.*PRD)\.(\d*)\./){
        my $topic_type_string = $1;
        my $topic_id = $2;
        print "$1\n$2\n";

    }
if($string =~ /(?(?=endid=)\d*$)/){
    # how to print match pattern here? 
    print "match\n";
}

提前致谢!

2 个答案:

答案 0 :(得分:2)

这将完成这项工作:

use Modern::Perl;
use Data::Dumper;

my $re = qr/(FPJ.+?PRD)\.(\d+)\..*?(\d+)$/;
while(<DATA>) {
    chomp;
    my (@l) = $_ =~  /$re/g;
    say Dumper\@l;
}

__DATA__
sequence_history/buckets/FPJ.INV_DOM_16_PRD.47269.2644?startid=2644000&endid=2644666
sequence_history/buckets/FPJ.INV_DOM_16_PRD.41987.9616

<强>输出:

$VAR1 = [
          'FPJ.INV_DOM_16_PRD',
          '47269',
          '2644666'
        ];

$VAR1 = [
          'FPJ.INV_DOM_16_PRD',
          '41987',
          '9616'
        ];

<强>解释

(       : start group 1
  FPJ   : literally FPJ
  .+?   : 1 or more any character but newline, not greedy
  PRD   : literally PRD
)       : end group 1
\.      : a dot
(       : start group 2
  \d+   : 1 or more digit
)       : end group 2
\.      : a dot
.*?     : 0 or more any character not greedy
(       : start group 3
  \d+   : 1 or more digit
)       : end group 3
$       : end of string

答案 1 :(得分:0)

如果您尝试在日志文件中获取某些条目,则可以在perl中使用文件句柄。在下面的代码中,我试图从名为test.log

的日志文件中获取条目

日志的条目如下。

sequence_history/buckets/FPJ.INV_DOM_16_PRD.47269.2644?startid=2644000&endid=2644666
sequence_history/buckets/FPJ.INV_DOM_16_PRD.41987.9616
sequence_history/buckets/FPJ.INV_DOM_16_PRD.47269.69886?startid=2644000&endid=26765849
sequence_history/buckets/FPJ.INV_DOM_16_PRD.47269.24465?startid=2644000&endid=836783741

以下是获取所需数据的perl脚本。

#!/usr/bin/perl

use strict;
use warnings;

open (FH, "test.log") || die "Not able to open test.log $!";

my ($a,$b,$c);
while (my $line=<FH>)
{

        if ($line =~ /sequence_history\/buckets\/.*endid=(\d*)/)
        {
                $c= $1;
                if ($line =~ /(FPJ.*PRD)\.(\d*)\.(\d*)\?/)
                {
                        $a=$1;
                        $b=$2;
                }
        }
        else
        {
                if ($line =~ /sequence_history\/buckets\/(FPJ.*PRD)\.(\d*)\.(\d*)/)
                {
                        $a=$1;
                        $b=$2;
                        $c=$3;
                }
        }

print "\n \$a=$a\n \$b=$b\n \$c=$c \n";
}

输出:

 $a=FPJ.INV_DOM_16_PRD
 $b=47269
 $c=2644666 

 $a=FPJ.INV_DOM_16_PRD
 $b=41987
 $c=9616 

 $a=FPJ.INV_DOM_16_PRD
 $b=47269
 $c=26765849 

 $a=FPJ.INV_DOM_16_PRD
 $b=47269
 $c=836783741 

您可以通过替换&#34; test.log&#34;来使用上述代码。通过日志文件名(及其路径)从中获取数据,如下所示。

open (FH, "/path/to/log/file/test.log") || die "Not able to open test.log $!";