perl中的嵌套正则表达式

时间:2015-04-01 01:29:01

标签: regex perl

我希望编写一个perl程序,它在模式中查找以下关键字:

`lint64 | strictlintflags64 | cflags64.`

一旦我确保搜索到的文本中存在其中一种模式,我就会深入挖掘,看看搜索到的文本中是否隐藏了任何异常。 异常是相同的单词没有最后附加的字符串'64'。 那就是:

lint | strictlintflags | cflags

例如: 纯字符串将是:

"asd:   lint64_SRCS"

异常链接的字符串将是:

"CFLAGS += $(CCVERBOSE)lint:     lint64_PROG"

以下是我编写的用于检测特定搜索文本是否存在异常的测试代码:

#!/usr/perl5/bin/perl -w

require 5.6.1;
use strict;
use warnings;
use locale;

my $string = "CFLAGS += $(CCVERBOSE)lint:     lint64_PROG";
#my $string2 = "asd:   lint64_SRCS";

if ($string =~ m/lint64|strictlintflags64|cflags64/ig) {
    if ($string =~ m/lint(?!64)|strictlintflags(?!64)|cflags(?!64)/ig) {
        print "anomaly-ridden"
    }
    else {
        print "pure";
    }
}
else {
    print "garbage";
}

此代码正确检测$ string2为纯字符串。

不幸的是,$string被视为“纯粹”而不是“异常情况”。

有关原因的任何线索? 另外,如果我注释掉外部if循环,则$string被正确检测为"anomaly-ridden"字符串

1 个答案:

答案 0 :(得分:1)

摆脱/ g标志;他们告诉它做第一场比赛,然后尝试第一场比赛结束的第二场比赛。