gpt4 book ai didi

变量之间的 Perl 混淆

转载 作者:行者123 更新时间:2023-12-02 05:52:17 33 4
gpt4 key购买 nike

我有以下代码

my $content = $response->content;
$content =~ /username=([\s\S]+?)&/;
my $username = $1;
print $username; #Prints the text

假设我想再做一次,但针对不同的文本

例如

$content =~ /rank=([\s\S]+?)&/;
my $rank = $1;
print $rank; #Prints the username text

我必须将 $1 换成其他东西吗?

最佳答案

my $content = $response->content;
$content =~ /username=([\s\S]+?)&/;
my $username = $1;
print $username; #Prints the text

$content =~ /rank=([\s\S]+?)&/;

#if the above regex does not match, $1 remains set to the previous $1

my $rank = $1;
print $rank; #Prints the username text

这样会更安全:

if ($content =~ /rank=([\s\S]+?)&/){
my $rank = $1;
}

或者,更优雅地:

my ($rank) = $content =~ /rank=([\s\S]+?)&/;
print "\n rank:$rank" if defined $rank; #Prints the username text

关于变量之间的 Perl 混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11307172/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com