1

我有以下代码:

#!/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

如您所见,它将数组中的单词插入到 url 的特定位置。

我现在遇到的问题:

我现在想添加功能来执行HELLOand 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

但我不知道如何以最好的方式解决这个问题?

非常感谢您对此的帮助,非常感谢

4

1 回答 1

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
于 2013-03-11T13:45:08.743 回答