regex - 在 Perl 中打印没有子字符串的字符串

标签 regex string perl

我有一个格式如下的日志文件:

[Time] [mmm] [DATA] [rule] [a.a.a.a]
[Time] [ppp] [DATA] [rule] [a.a.a.a]
[Time] [mmm] [DATA] [rule] [c.c.c.c]

在无法'找到一种方法来打印没有特定子字符串的这个字符串。我希望能够在没有匹配 [mmm] 的子字符串行的情况下打印整个字符串输出和 [a.a.a.a] .最终输出将是:
[Time] [ppp] [DATA] [rule] [a.a.a.a]  
[Time] [mmm] [DATA] [rule] [c.c.c.c]

我是使用带有两个子字符串的索引模块还是以某种方式使用 grep?我看错了吗?任何帮助深表感谢!!!

在我的 perl 脚本中,我有一个部分搜索此部分并将此部分打印为字符串:
sub Section
{
    my @event_rule = ("A", "B", "C");

    foreach (@event_rule)
    {
                    my $result1 = `fgrep -h 'DATA' $logfile1 | grep "$_" | head -n10`;
                    if (length $result1)
                    {
                        print "$result1\n";
            }
            }
  }

最佳答案

不需要像 grep 这样的外部程序:

#!/usr/bin/perl
use warnings;
use strict;

my @rule  = ('[mmm]', '[a.a.a.a]');
my $regex = join '.*', map quotemeta, @rule; # Create one regular expression from the "rules".
$regex    = qr/$regex/;                      # Compile it.

my $c = 0;
while (<>) {
    $c += print if /DATA/ && ! /$regex/;
    last if $c > 9;                          # Only print the first 10 lines.
}

关于regex - 在 Perl 中打印没有子字符串的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30233389/

相关文章:

java - 在不使用 Excel 的情况下为任意长度的字符串设置图表格式的最佳方法

perl - 如何在Perl中运行匿名函数?

Perl WWW::Mechanize 编码问题

perl - 你如何停止 perl Dancer/Starman/Plack 服务器?

php - 如何使用正则表达式获取所有属性?

javascript - JavaScript 中的正则表达式匹配错误

string - 我如何将 Option<char> 附加到字符串?

python - 如何在 python 中使用 re.findall

regex - sed:从最后一次出现的 "-"替换到 EOL

javascript - 使用 .split() 将字符串转换为数组(需要改进)