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 の特定の場所に配列内の単語が挿入されます。

私が今問題を抱えていること:

HELLOGOODBYE(またはにあるもの)のすべての可能な組み合わせを実行する機能を追加したいと思います。@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 です。各パラメーターは、元の値を持つことも、2 つの値のいずれかを先行、後続、または置換することもできます。これは、各パラメーターに対して 7 つの可能な値であるため、2 つのパラメーターに対して 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 に答える