2

我正在尝试将 /ezmlm-(除 'weed' 或 'return')\s+/ 与正则表达式匹配。下面演示了一个做正确事情的 foreach 循环,以及一个几乎可以做的尝试的正则表达式:

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

my @tests = (
    {  msg => "want 'yes', string has ezmlm, but not weed or return",
       str => q[|/usr/local/bin/ezmlm-reject '<snip>'],
    },
    {  msg => "want 'yes', array  has ezmlm, but not weed or return",
       str => [ <DATA> ],
    },
    {  msg => "want 'no' , has ezmlm-weed",
       str => q[|/usr/local/bin/ezmlm-weed '<snip>'],
    },
    {  msg => "want 'no' , doesn't have ezmlm-anything",
       str => q[|/usr/local/bin/else '<snip>'],
    },
    {  msg => "want 'no' , ezmlm email pattern",
       str => q[crazy/but/legal/ezmlm-wacky@example.org],
    },
);

print "foreach regex\n";
foreach ( @tests ) {
    print doit_fe( ref $_->{str} ? @{$_->{str}} : $_->{str} ) ? "yes" : "no";
    print "\t";
    print doit_re( ref $_->{str} ? @{$_->{str}} : $_->{str} ) ? "yes" : "no";
    print "\t<--- $_->{msg}\n";
};

# for both of the following subs:
#   @_ will contain one or more lines of data
#   match the pattern /ezmlm-(any word except 'weed' or 'return')\s+/

sub doit_fe {
    my $has_ezmlm = 0;
    foreach ( @_ ) {
        next if $_ !~ m/ezmlm-(.*?)\s/;
        return 0 if $1 eq 'weed' or $1 eq 'return';
        $has_ezmlm++;
    };
    return $has_ezmlm;
};

sub doit_re { return grep /ezmlm-(?!weed|return)/, @_; };

__DATA__
|/usr/local/bin/ezmlm-reject '<snip>'
|/usr/local/bin/ezmlm-issubn '<snip>'
|/usr/local/bin/ezmlm-send '<snip>'
|/usr/local/bin/ezmlm-archive '<snip>'
|/usr/local/bin/ezmlm-warn '<snip>'

示例程序的输出如下:

foreach regex
yes yes <--- want 'yes', string has ezmlm, but not weed or return
yes yes <--- want 'yes', array  has ezmlm, but not weed or return
no  no  <--- want 'no' , has ezmlm-weed
no  no  <--- want 'no' , doesn't have ezmlm-anything
no  yes <--- want 'no' , ezmlm email pattern

在最后一个例子中,正则表达式失败,匹配一个愚蠢但合法的电子邮件地址。如果我修改正则表达式,在负前瞻模式之后放置一个 \s,如下所示:

grep /ezmlm-(?!weed|return)\s+/

正则表达式根本不匹配。我认为它与负面模式的工作方式有关。我试过让否定变得不贪婪,但似乎有一些隐藏在“perldoc perlre”中的教训正在逃避我。是否可以使用单个正则表达式来做到这一点?

4

1 回答 1

4

负前瞻是零宽度,这意味着正则表达式

/ezmlm-(?!weed|return)\s+/

仅当紧跟一个或多个空格字符时才会匹配"ezmlm-"

图案

/ezmlm-(?!weed|return)/

将匹配

"crazy/but/legal/ezmlm-wacky@example.org"

因为它"ezmlm-"后面不包含"weedy"or "return"

尝试

/ezmlm-(?!weed|return)\S+\s+/

where\S+是一个或多个非空格字符(或者[^@\s]+如果您想拒绝电子邮件地址,即使后跟空格也可以使用)。

于 2013-04-10T22:38:17.457 回答