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

Regex Perl正则表达式-打印匹配的条件正则表达式,regex,perl,conditional,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 我想摘录三件事: A=“FPJ.INV\u DOM\u 16\u PRD”B=“47269”C=9616或2644666(如

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

日志字符串的示例:

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
我想摘录三件事:

A=“FPJ.INV\u DOM\u 16\u PRD”B=“47269”C=9616或2644666(如果行 有endid,然后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";
}
提前谢谢

这将完成工作:

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

如果您试图获取日志文件中的一些条目,那么可以使用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";
}
通过将“test.log”替换为要从中获取数据的日志文件名(及其路径),可以使用上述代码,如下所示

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

像这样的?谢谢。工作完美。另外,谢谢您对正则表达式的解释。@PushpinderSingh:不客气,很高兴它能帮上忙。请随意将答案标记为已接受,请参见:
open (FH, "/path/to/log/file/test.log") || die "Not able to open test.log $!";