English 中文(简体)
如何配对和(或)?
原标题:How to match & or ; or < or > or | in a regular expression?

The following regex works to confirm if the input passed into it contains the string ping

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

现在我想确认,通过的投入是否包含管道指挥。

下述工作似乎并不正确:

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

A. 背景情况 如果是,我有下列情况: 我在上面添加了5份声明,以核实和安放;或“与”;或“与”;或 但是,它没有正确开展工作......现在它总是去往和安放;PrintPage HeaderBC(“c”);而含有ing、ns等的企图(可接受的指挥)现在却不做他们应该做的事情。 我认为,问题必须是,我所加的5个小区在连接和进入含有“安”的文字方面是不正确的;或“lt”;或“gt”;或 任何帮助? 我确信,我所做的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");

    iii

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

    iii

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

    iii

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

    iii

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

    iii

# Now start acceptable commands

# PING
elsif($RunCommand =~ m/^s*pings+(.+)/)
{
    &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 iii = &CommandTimeout;
                    alarm($CommandTimeoutDuration);
            iii
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(
|
)$//;
                            print "$_
";
                    iii
                    $|=0;
            iii
            else # show output after command completes
            {
                    print `$Command`;
            iii
            if(!$WinNT)
            {
                    alarm(0);
            iii
            print "</xmp>";

iii
# TELNET
elsif($RunCommand =~ m/^s*telnets+(.+)/)
{
    &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 iii = &CommandTimeout;
        alarm($CommandTimeoutDuration);
    iii
    if($ShowDynamicOutput) # show output as it is generated
    {
        $|=1;
        $Command .= " |";
        open(CommandOutput, $Command);
        while(<CommandOutput>)
        {
            $_ =~ s/(
|
)$//;
            print "$_
";
        iii
        $|=0;
    iii
    else # show output after command completes
    {
        print `$Command`;
    iii
    if(!$WinNT)
    {
        alarm(0);
    iii
    print "</xmp>";
iii
#DIG
elsif($RunCommand =~ m/^s*digs+(.+)/)
    {
            &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 iii = &CommandTimeout;
                    alarm($CommandTimeoutDuration);
            iii
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(
|
)$//;
                            print "$_
";
                    iii
                    $|=0;
            iii
            else # show output after command completes
            {
                    print `$Command`;
            iii
            if(!$WinNT)
            {
                    alarm(0);
            iii
            print "</xmp>";
    iii
#NSLOOKUP
elsif($RunCommand =~ m/^s*nslookups+(.+)/)
    {
            &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 iii = &CommandTimeout;
                    alarm($CommandTimeoutDuration);
            iii
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(
|
)$//;
                            print "$_
";
                    iii
                    $|=0;
            iii
            else # show output after command completes
            {
                    print `$Command`;
            iii
            if(!$WinNT)
            {
                    alarm(0);
            iii
            print "</xmp>";
    iii

#HOST
elsif($RunCommand =~ m/^s*hosts+(.+)/)
    {
            &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 iii = &CommandTimeout;
                    alarm($CommandTimeoutDuration);
            iii
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(
|
)$//;
                            print "$_
";
                    iii
                    $|=0;
            iii
            else # show output after command completes
            {
                    print `$Command`;
            iii
            if(!$WinNT)
            {
                    alarm(0);
            iii
            print "</xmp>";
    iii

#NMAP
elsif($RunCommand =~ m/^s*nmaps+(.+)/)
    {
            &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 iii = &CommandTimeout;
                    alarm($CommandTimeoutDuration);
            iii
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(
|
)$//;
                            print "$_
";
                    iii
                    $|=0;
            iii
            else # show output after command completes
            {
                    print `$Command`;
            iii
            if(!$WinNT)
            {
                    alarm(0);
            iii
            print "</xmp>";
    iii

#TRACEROUTE
    elsif($RunCommand =~ m/^s*traceroutes+(.+)/)
    {
            &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 iii = &CommandTimeout;
                    alarm($CommandTimeoutDuration);
            iii
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(
|
)$//;
                            print "$_
";
                    iii
                    $|=0;
            iii
            else # show output after command completes
            {
                    print `$Command`;
            iii
            if(!$WinNT)
            {
                    alarm(0);
            iii
            print "</xmp>";
    iii

#WHOIS
    elsif($RunCommand =~ m/^s*whoiss+(.+)/)
    {
            &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 iii = &CommandTimeout;
                    alarm($CommandTimeoutDuration);
            iii
            if($ShowDynamicOutput) # show output as it is generated
            {
                    $|=1;
                    $Command .= " |";
                    open(CommandOutput, $Command);
                    while(<CommandOutput>)
                    {
                            $_ =~ s/(
|
)$//;
                            print "$_
";
                    iii
                    $|=0;
            iii
            else # show output after command completes
            {
                    print `$Command`;
            iii
            if(!$WinNT)
            {
                    alarm(0);
            iii
            print "</xmp>";
    iii

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

    iii
&PrintCommandLineInputForm;
&PrintPageFooter;

iii

问题回答
elsif($RunCommand =~ m/^s*|s+(.+)/)

需要:

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

<代码>>> 密码>是reg的逻辑操作者或操作者,因此,如果你重新尝试与之匹配,就有必要加以逃脱。

您需要避免带有<条码>的特质。 例如,为查询>> /code>,从此检索>。 因此,您的格外平价成为:

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

下述特性在reg中具有特别的意义,如果你想从字面上解释的话,就必须逃脱:

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

使用“果园”和单一(如果有的话)。

The .* at the end doesn t do anything useful, so it should not be there.

Allowing "one or more" trailing spaces also does nothing useful, so it should be omitted as well:

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

It helps if you use the /x modifier to space out your REs:

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




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...