我有以下代码:
#!/usr/bin/perl
use strict;
use warnings;
#use diagnostics;
use URI qw( );
my @insert_words = qw(HELLO GOODBYE);
while (<DATA>) {
chomp;
my $url = URI->new($_);
my $query = $url->query;
foreach (@insert_words) {
# Use package vars to communicate with /(?{})/ blocks.
local our $insert_word = $_;
local our @queries;
if (defined $query) {
$query =~ m{
^(.*[/=])([^/=&]*)((?:[/=&].*)?)\z
(?{
if (length $2) {
push @queries, "$1$insert_word$2$3";
push @queries, "$1$insert_word$3";
push @queries, "$1$2$insert_word$3";
}
})
(?!)
}x;
}
if (@queries) {
for (@queries) {
$url->query($_);
print $url, "\n";
}
}
else {
print $url, "\n";
}
}
}
__DATA__
http://www.example.com/index.php?route=9&other=7
上面的代码正常工作并产生以下输出:
http://www.example.com/index.php?route=9&other=HELLO7
http://www.example.com/index.php?route=9&other=HELLO
http://www.example.com/index.php?route=9&other=7HELLO
http://www.example.com/index.php?route=HELLO9&other=7
http://www.example.com/index.php?route=HELLO&other=7
http://www.example.com/index.php?route=9HELLO&other=7
http://www.example.com/index.php?route=9&other=GOODBYE7
http://www.example.com/index.php?route=9&other=GOODBYE
http://www.example.com/index.php?route=9&other=7GOODBYE
http://www.example.com/index.php?route=GOODBYE9&other=7
http://www.example.com/index.php?route=GOODBYE&other=7
http://www.example.com/index.php?route=9GOODBYE&other=7
如您所见,它会在数组中的特定位置插入单词中的单词。
我现在遇到的问题是:
我现在想要添加功能来执行HELLO
和GOODBYE
(或@insert_words
中的任何内容)的所有可能组合,例如它还应添加以下url是我已经得到的输出:
http://www.example.com/index.php?route=HELLO&other=GOODBYE
http://www.example.com/index.php?route=HELLO&other=HELLO
http://www.example.com/index.php?route=GOODBYE&other=HELLO
http://www.example.com/index.php?route=GOODBYE&other=GOODBYE
但我不知道如何以最好的方式解决这个问题?
非常感谢您对此的帮助,非常感谢
答案 0 :(得分:2)
请不要使用类似花哨的正则表达式 - 它们是Perl的实验性功能,并且远非易于理解。
如果我理解你,那么你需要递归地执行此操作。
我认为您希望URL的所有变体与每个查询参数一样,或者先于,成功,或者被@insert_words
中的每个值替换。
这似乎符合你的要求。它使用URI::QueryParam
正确分割URL的查询部分,而不是使用令人讨厌的正则表达式。它确实产生了比您在问题中显示的组合更多的组合,但我看不到其他方式来解释您的要求。
可能的变化数量为49.每个参数可以具有其原始值,或者在两个值之一之前,之后成功或替换。这是每个参数的七个可能值,因此两个参数有7个或49个不同的变化。
use strict;
use warnings;
use URI;
use URI::QueryParam;
my @insert_words = qw/ HELLO GOODBYE /;
my @urls;
sub mod_param {
my ($url, $paridx, @insertions) = @_;
my @params = $url->query_param;
return if $paridx > $#params;
my $key = $params[$paridx];
my $oldval = $url->query_param($key);
my @variations = ($oldval);
push @variations, ($oldval.$_, $_.$oldval, $_) for @insertions;
for my $val (@variations) {
$url->query_param($key, $val);
if ($paridx == $#params) {
push @urls, "$url";
}
else {
mod_param($url, $paridx + 1, @insertions);
}
}
$url->query_param($key, $oldval);
}
while (<DATA>) {
chomp;
my $url = URI->new($_);
@urls = ();
mod_param($url, 0, @insert_words);
print $_, "\n" for @urls;
}
__DATA__
http://www.example.com/index.php?route=9&other=7
<强>输出强>
http://www.example.com/index.php?route=9&other=7
http://www.example.com/index.php?route=9&other=7HELLO
http://www.example.com/index.php?route=9&other=HELLO7
http://www.example.com/index.php?route=9&other=HELLO
http://www.example.com/index.php?route=9&other=7GOODBYE
http://www.example.com/index.php?route=9&other=GOODBYE7
http://www.example.com/index.php?route=9&other=GOODBYE
http://www.example.com/index.php?route=9HELLO&other=7
http://www.example.com/index.php?route=9HELLO&other=7HELLO
http://www.example.com/index.php?route=9HELLO&other=HELLO7
http://www.example.com/index.php?route=9HELLO&other=HELLO
http://www.example.com/index.php?route=9HELLO&other=7GOODBYE
http://www.example.com/index.php?route=9HELLO&other=GOODBYE7
http://www.example.com/index.php?route=9HELLO&other=GOODBYE
http://www.example.com/index.php?route=HELLO9&other=7
http://www.example.com/index.php?route=HELLO9&other=7HELLO
http://www.example.com/index.php?route=HELLO9&other=HELLO7
http://www.example.com/index.php?route=HELLO9&other=HELLO
http://www.example.com/index.php?route=HELLO9&other=7GOODBYE
http://www.example.com/index.php?route=HELLO9&other=GOODBYE7
http://www.example.com/index.php?route=HELLO9&other=GOODBYE
http://www.example.com/index.php?route=HELLO&other=7
http://www.example.com/index.php?route=HELLO&other=7HELLO
http://www.example.com/index.php?route=HELLO&other=HELLO7
http://www.example.com/index.php?route=HELLO&other=HELLO
http://www.example.com/index.php?route=HELLO&other=7GOODBYE
http://www.example.com/index.php?route=HELLO&other=GOODBYE7
http://www.example.com/index.php?route=HELLO&other=GOODBYE
http://www.example.com/index.php?route=9GOODBYE&other=7
http://www.example.com/index.php?route=9GOODBYE&other=7HELLO
http://www.example.com/index.php?route=9GOODBYE&other=HELLO7
http://www.example.com/index.php?route=9GOODBYE&other=HELLO
http://www.example.com/index.php?route=9GOODBYE&other=7GOODBYE
http://www.example.com/index.php?route=9GOODBYE&other=GOODBYE7
http://www.example.com/index.php?route=9GOODBYE&other=GOODBYE
http://www.example.com/index.php?route=GOODBYE9&other=7
http://www.example.com/index.php?route=GOODBYE9&other=7HELLO
http://www.example.com/index.php?route=GOODBYE9&other=HELLO7
http://www.example.com/index.php?route=GOODBYE9&other=HELLO
http://www.example.com/index.php?route=GOODBYE9&other=7GOODBYE
http://www.example.com/index.php?route=GOODBYE9&other=GOODBYE7
http://www.example.com/index.php?route=GOODBYE9&other=GOODBYE
http://www.example.com/index.php?route=GOODBYE&other=7
http://www.example.com/index.php?route=GOODBYE&other=7HELLO
http://www.example.com/index.php?route=GOODBYE&other=HELLO7
http://www.example.com/index.php?route=GOODBYE&other=HELLO
http://www.example.com/index.php?route=GOODBYE&other=7GOODBYE
http://www.example.com/index.php?route=GOODBYE&other=GOODBYE7
http://www.example.com/index.php?route=GOODBYE&other=GOODBYE