如何使用存储在另一个变量中的索引获取Perl regex match变量的值?

如何使用存储在另一个变量中的索引获取Perl regex match变量的值?,perl,Perl,我有一个子程序,它以字符串中的一个位置作为输入,并应返回在该位置找到的单词。例如: use warnings; use strict; my $num=2; my $val=getMatch($num); sub getMatch { my ($num)=@_; my $str='a b c'; $str=~ /(\S+)\s(\S+)/; my $res; eval "$res=\$$num"; return $res } 但这会产生错

我有一个子程序,它以字符串中的一个位置作为输入,并应返回在该位置找到的单词。例如:

use warnings;
use strict;

my $num=2;
my $val=getMatch($num);

sub getMatch {
    my ($num)=@_;

    my $str='a b c';
    $str=~ /(\S+)\s(\S+)/;

    my $res;
    eval "$res=\$$num";
    return $res
}
但这会产生错误:

Use of uninitialized value $res in concatenation (.) or string at ./p.pl line 16.
我试图返回$I,其中I是存储在另一个变量中的值。

我会:

my $num=2;
my $val=getMatch($num);
say $val;
sub getMatch {
    my ($num)=@_;
    my $str='a b c';
    my @res = $str =~ /(\S+)\s(\S+)/;
    return $res[$num-1];
}
输出:

我会:

my $num=2;
my $val=getMatch($num);
say $val;
sub getMatch {
    my ($num)=@_;
    my $str='a b c';
    my @res = $str =~ /(\S+)\s(\S+)/;
    return $res[$num-1];
}
输出:

您可以使用中记录的@+和@-特殊变量,如下所示:

sub getMatch {
    my ($num)=@_;

    my $str='a b c';
    $str=~ /(\S+)\s(\S+)/;

    return substr( $str, $-[$num], $+[$num] - $-[$num] );
}
print getMatch(1), "\n";
print getMatch(2), "\n";
sub getMatch {
    my $num = shift() - 1;
    my $str='a b c';
    $str=~ /(?:\S+\s){$num}(\S+)/;

    return $1;
}
print getMatch(1), "\n";
print getMatch(2), "\n";
或者你可以像这样调整你的正则表达式:

sub getMatch {
    my ($num)=@_;

    my $str='a b c';
    $str=~ /(\S+)\s(\S+)/;

    return substr( $str, $-[$num], $+[$num] - $-[$num] );
}
print getMatch(1), "\n";
print getMatch(2), "\n";
sub getMatch {
    my $num = shift() - 1;
    my $str='a b c';
    $str=~ /(?:\S+\s){$num}(\S+)/;

    return $1;
}
print getMatch(1), "\n";
print getMatch(2), "\n";
…其优点是只产生一个捕获组

另一种选择是仅在空间上拆分:

sub getMatch {
    my ($num)=@_;
    my $str='a b c';
    return ( split /\s/, $str )[$num-1];
}

print getMatch(1), "\n";
print getMatch(2), "\n";
…但最后一种解决方案在匹配什么方面更为宽松;它并不明确要求两个或多个由空格分隔的非空格项。如果传入3,它将返回“c”

最后一个生成的结果类似于拆分版本,但使用正则表达式。我可能更喜欢拆分,因为它更简单,但我提供此选项只是为了启发:

sub getMatch {
    my ($num)=@_;
    my $str='a b c';
    return ( $str =~ m/(\S+)(?=\s|$)/g )[$num-1];
}

print getMatch(1), "\n";
print getMatch(2), "\n";
您可以使用中记录的@+和@-特殊变量,如下所示:

sub getMatch {
    my ($num)=@_;

    my $str='a b c';
    $str=~ /(\S+)\s(\S+)/;

    return substr( $str, $-[$num], $+[$num] - $-[$num] );
}
print getMatch(1), "\n";
print getMatch(2), "\n";
sub getMatch {
    my $num = shift() - 1;
    my $str='a b c';
    $str=~ /(?:\S+\s){$num}(\S+)/;

    return $1;
}
print getMatch(1), "\n";
print getMatch(2), "\n";
或者你可以像这样调整你的正则表达式:

sub getMatch {
    my ($num)=@_;

    my $str='a b c';
    $str=~ /(\S+)\s(\S+)/;

    return substr( $str, $-[$num], $+[$num] - $-[$num] );
}
print getMatch(1), "\n";
print getMatch(2), "\n";
sub getMatch {
    my $num = shift() - 1;
    my $str='a b c';
    $str=~ /(?:\S+\s){$num}(\S+)/;

    return $1;
}
print getMatch(1), "\n";
print getMatch(2), "\n";
…其优点是只产生一个捕获组

另一种选择是仅在空间上拆分:

sub getMatch {
    my ($num)=@_;
    my $str='a b c';
    return ( split /\s/, $str )[$num-1];
}

print getMatch(1), "\n";
print getMatch(2), "\n";
…但最后一种解决方案在匹配什么方面更为宽松;它并不明确要求两个或多个由空格分隔的非空格项。如果传入3,它将返回“c”

最后一个生成的结果类似于拆分版本,但使用正则表达式。我可能更喜欢拆分,因为它更简单,但我提供此选项只是为了启发:

sub getMatch {
    my ($num)=@_;
    my $str='a b c';
    return ( $str =~ m/(\S+)(?=\s|$)/g )[$num-1];
}

print getMatch(1), "\n";
print getMatch(2), "\n";

好像我忘了在$res前面加斜杠:eval\$res=\$$num。。但也许有更简单的方法可以做到这一点?好像我忘了在$res前面加一个斜杠:eval\$res=\$$num。。但也许有更简单的方法可以做到这一点?