开发者

How to refer regular expression matches evaluated in one statement?

I want to do something like this

if(($Fifo[5]=~/T0int(\S+)/)&&($Fifo[6开发者_如何学Go]=~/T0int(\S+)/)&&($1 ne $2))
{
    <Do something>
}

How can I reference matches evaluated in two regexps ?

By $1 I meant match evaluated in the first regexp and $2 in the next.


my($first)  = $Fifo[5] =~ /T0int(\S+)/;
my($second) = $Fifo[6] =~ /T0int(\S+)/;

if (defined($first) && defined($second) && $first ne $second)) { ⋯ }

or more cavalierly:

if (($Fifo[5] =~ /T0int(\S+)/)[0] ne ($Fifo[6] =~ /T0int(\S+)/)[0]) { ⋯ }

or even more cavalierly still:

if ( (my($first,   $second) = "@Fifo[5,6]" =~ /T0int(\S+)/g ) 
      && $first && $second
      && $first ne $second)
{
    ⋯
}


Try something like this:

if( ($Fifo[5] =~ (/T0int(\S+)/)) && ($Fifo[6] =~ (/T0int(\S+)/)) && ($1 ne $2) )

Basically put parenthesis around regex to group them as $1, $2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜