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

标签 regex perl conditional-statements

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

日志字符串示例:

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 or 2644666 (if the line has endid then C = 2644666 else it's 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

关于regex - Perl Regex - 打印匹配的条件正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45332870/

相关文章:

jQuery:使用 .replace() 替换元素内的文本

regex - 更正 bash sed 命令语法以获得正确的子字符串

windows - 如何测试 STDIN 是否有可读取的内容(Windows 上的 Perl)

perl - 模块::构建缓存清理

Javascript 很奇怪,如果其他不工作

python - 使用条件,在 pandas DataFrame 中选择所需的列

r - 从R中的单独数据帧中提取匹配多个条件的ID列表

javascript - 将函数调用与 JS Regex 匹配

javascript - 密码正则表达式来阻止方括号和空格?

perl - 使用 wget 和 Perl 脚本从网页中提取信息