如何匹配&要么 ;或者<或者>或者在正则表达式?

时间:2011-08-28 16:38:01

标签: regex perl syntax

以下正则表达式用于确认传入其中的输入是否包含字符串'ping'

elsif($RunCommand =~ m/^\s*ping\s+(.+)/)

现在我想确认传递的输入是否包含管道命令|

以下似乎无法正常工作:

elsif($RunCommand =~ m/^\s*|\s+(.+)/)

对于上下文,我有以下if ifif子程序。我刚刚在其顶部添加了5个语句,用于检查&要么 ;或者<或者>或者。但是它没有正常工作......现在它总是进入& PrintPageHeaderBC(“c”);并且包含ping,nslookup等(可接受的命令)的尝试现在不能执行它们应该执行的操作。我认为问题必定是我添加的5个正则表达不匹配和输入的文本包含&要么 ;或者<或者>或者。有帮助吗?我确信我所做的5个单独陈述(可能是错误的)也可以合并为一个陈述。

# First discard command attempts that contain & ; < > |, then acceptable commands are executed, then final else to non-functional command

 if($RunCommand =~ m/^\s*&\s+(.+)/)
    {
            # Print PageHeaderBC that informs of non-functional command
            &PrintPageHeaderBC("c");

    }

 elsif($RunCommand =~ m/^\s*;\s+(.+)/)
    {
            # Print PageHeaderBC that informs of non-functional command
            &PrintPageHeaderBC("c");

    }

 elsif($RunCommand =~ m/^\s*<\s+(.+)/)
    {
            # Print PageHeaderBC that informs of non-functional command
            &PrintPageHeaderBC("c");

    }

 elsif($RunCommand =~ m/^\s*>\s+(.+)/)
    {
            # Print PageHeaderBC that informs of non-functional command
            &PrintPageHeaderBC("c");

    }

 elsif($RunCommand =~ m/^\s*|\s+(.+)/)
    {
            # Print PageHeaderBC that informs of non-functional command
            &PrintPageHeaderBC("c");

    }

# Now start acceptable commands

# PING
elsif($RunCommand =~ m/^\s*ping\s+(.+)/)
{
    &PrintPageHeader("c");
            #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
            $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
            print "<code>$Prompt $RunCommand</code><xmp>";
            $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
            if(!$WinNT)
            {
                    $SIG{'ALRM'} = \&CommandTimeout;
                    alarm($CommandTimeoutDuration);
            }
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(\n|\r\n)$//;
                            print "$_\n";
                    }
                    $|=0;
            }
            else # show output after command completes
            {
                    print `$Command`;
            }
            if(!$WinNT)
            {
                    alarm(0);
            }
            print "</xmp>";

}
# TELNET
elsif($RunCommand =~ m/^\s*telnet\s+(.+)/)
{
    &PrintPageHeader("c");
    #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
    $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
    print "<code>$Prompt $RunCommand</code><xmp>";
    $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
    if(!$WinNT)
    {
        $SIG{'ALRM'} = \&CommandTimeout;
        alarm($CommandTimeoutDuration);
    }
    if($ShowDynamicOutput) # show output as it is generated
    {
        $|=1;
        $Command .= " |";
        open(CommandOutput, $Command);
        while(<CommandOutput>)
        {
            $_ =~ s/(\n|\r\n)$//;
            print "$_\n";
        }
        $|=0;
    }
    else # show output after command completes
    {
        print `$Command`;
    }
    if(!$WinNT)
    {
        alarm(0);
    }
    print "</xmp>";
}
#DIG
elsif($RunCommand =~ m/^\s*dig\s+(.+)/)
    {
            &PrintPageHeader("c");
            #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
            $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
            print "<code>$Prompt $RunCommand</code><xmp>";
            $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
            if(!$WinNT)
            {
                    $SIG{'ALRM'} = \&CommandTimeout;
                    alarm($CommandTimeoutDuration);
            }
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(\n|\r\n)$//;
                            print "$_\n";
                    }
                    $|=0;
            }
            else # show output after command completes
            {
                    print `$Command`;
            }
            if(!$WinNT)
            {
                    alarm(0);
            }
            print "</xmp>";
    }
#NSLOOKUP
elsif($RunCommand =~ m/^\s*nslookup\s+(.+)/)
    {
            &PrintPageHeader("c");
            #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
            $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
            print "<code>$Prompt $RunCommand</code><xmp>";
            $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
            if(!$WinNT)
            {
                    $SIG{'ALRM'} = \&CommandTimeout;
                    alarm($CommandTimeoutDuration);
            }
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(\n|\r\n)$//;
                            print "$_\n";
                    }
                    $|=0;
            }
            else # show output after command completes
            {
                    print `$Command`;
            }
            if(!$WinNT)
            {
                    alarm(0);
            }
            print "</xmp>";
    }

#HOST
elsif($RunCommand =~ m/^\s*host\s+(.+)/)
    {
            &PrintPageHeader("c");
            #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
            $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
            print "<code>$Prompt $RunCommand</code><xmp>";
            $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
            if(!$WinNT)
            {
                    $SIG{'ALRM'} = \&CommandTimeout;
                    alarm($CommandTimeoutDuration);
            }
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(\n|\r\n)$//;
                            print "$_\n";
                    }
                    $|=0;
            }
            else # show output after command completes
            {
                    print `$Command`;
            }
            if(!$WinNT)
            {
                    alarm(0);
            }
            print "</xmp>";
    }

#NMAP
elsif($RunCommand =~ m/^\s*nmap\s+(.+)/)
    {
            &PrintPageHeader("c");
            #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
            $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
            print "<code>$Prompt $RunCommand</code><xmp>";
            $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
            if(!$WinNT)
            {
                    $SIG{'ALRM'} = \&CommandTimeout;
                    alarm($CommandTimeoutDuration);
            }
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(\n|\r\n)$//;
                            print "$_\n";
                    }
                    $|=0;
            }
            else # show output after command completes
            {
                    print `$Command`;
            }
            if(!$WinNT)
            {
                    alarm(0);
            }
            print "</xmp>";
    }

#TRACEROUTE
    elsif($RunCommand =~ m/^\s*traceroute\s+(.+)/)
    {
            &PrintPageHeader("c");
            #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
            $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
            print "<code>$Prompt $RunCommand</code><xmp>";
            $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
            if(!$WinNT)
            {
                    $SIG{'ALRM'} = \&CommandTimeout;
                    alarm($CommandTimeoutDuration);
            }
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(\n|\r\n)$//;
                            print "$_\n";
                    }
                    $|=0;
            }
            else # show output after command completes
            {
                    print `$Command`;
            }
            if(!$WinNT)
            {
                    alarm(0);
            }
            print "</xmp>";
    }

#WHOIS
    elsif($RunCommand =~ m/^\s*whois\s+(.+)/)
    {
            &PrintPageHeader("c");
            #$Prompt = $WinNT ? "$CurrentDir> " : "[admin\@$ServerName $CurrentDir]\$ ";
            $Prompt = $WinNT ? "$CurrentDir> " : "\$ ";
            print "<code>$Prompt $RunCommand</code><xmp>";
            $Command = "cd \"$CurrentDir\"".$CmdSep.$RunCommand.$Redirector;
            if(!$WinNT)
            {
                    $SIG{'ALRM'} = \&CommandTimeout;
                    alarm($CommandTimeoutDuration);
            }
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(\n|\r\n)$//;
                            print "$_\n";
                    }
                    $|=0;
            }
            else # show output after command completes
            {
                    print `$Command`;
            }
            if(!$WinNT)
            {
                    alarm(0);
            }
            print "</xmp>";
    }

else
{
            # Print PageHeaderBC that informs of non-functional command
    &PrintPageHeaderBC("c");

    }
&PrintCommandLineInputForm;
&PrintPageFooter;

}

6 个答案:

答案 0 :(得分:6)

|符号用于正则表达式中的alternation。如果要匹配文字|字符,则需要转义它:

m/^\s*\|\s+(.+)/

另外,如果我想冒险猜测,我会说它总是落入“失败”子程序的原因是因为正则表达式。通过交替(如所写),它匹配以\s* OR \s+(.+)开头的任何字符串,这实际上是由\s*中的星号运算符引起的任何字符串。

修改:关于您的评论,由于锚定(^),您的任何正则表达都不会匹配这些示例。如果我们以字符串ping 4.2.2.2; pwd为例,那么正则表达式引擎会在字符串的开头开始匹配,因为它是锚定的。初始\s*通过重复零次正确匹配,在这种情况下实际上无效。然后它会查找要匹配的;,但下一个字符是4,因此会失败。您提供的第二个字符串也是如此。如果您使用;之后\s+之后没有空格,它也会中断。

老实说,如果你真的想要阻止任何包含字符<>;|或{{1最简单的方法是将它们全部粘贴到一个字符类中并使用&进行匹配。由于此正则表达式是未锚定的,因此它将在字符串中的任何位置匹配。但这完全不允许使用这些字符,这意味着它们不能出现在字符串或任何类型的字符串中。这可能不是您想要的,但它将适用于您当前声明的规则(“五个正则表达式应匹配任何包含&amp;或;或&lt;或&gt;或|”的文本)。

答案 1 :(得分:3)

elsif($RunCommand =~ m/^\s*|\s+(.+)/)

需要:

elsif($RunCommand =~ m/^\s*\|\s+(.+)/)

|是正则表达式中的逻辑“或”运算符,因此如果您尝试匹配它,则需要将其转义

答案 2 :(得分:2)

您需要使用\转义特殊字符。例如,要搜索|,请将其转义为\|。所以你的正则表达式匹配变为:

m/^\s*\|\s+(.+)/

以下字符在正则表达式中都有特殊含义,如果您希望对它们进行字面解释,则需要进行转义:

* ? + [ ] ( ) { } ^ $ | \

答案 3 :(得分:1)

使用“字符类”和单个if。

。*最后没有做任何有用的事情,所以它不应该在那里。

允许“一个或多个”尾随空格也没有用,所以它 也应该省略:

if ($RunCommand =~ m/^\s*[&;<>|]\s/)

答案 4 :(得分:0)

你需要逃脱管道角色,就像eldarerathis所说的那样。 您还可能希望将前5个案例组合成字符类:

if($RunCommand =~ m/^\s*[&;><\|]\s+(.+)/) {
    PrintPageHeaderBC("c");
}

这将为您节省大量的空间和时间进行维护。

[&;><\|]将匹配任何指定的字符。

阅读Bracketed Character Classes了解更多信息:

答案 5 :(得分:0)

如果您使用/ x修饰符来区分RE,这会有所帮助:

m/^ \s* \& \s+ (.+) /x