Perl用更简单的方法替换复杂的正则表达式

时间:2013-03-12 12:42:38

标签: regex perl url

我有以下代码:

#!/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    <-- precedes the query parameter
http://www.example.com/index.php?route=9&other=HELLO     <-- replaces the query parameter
http://www.example.com/index.php?route=9&other=7HELLO    <-- succeeds the query parameter and so on for the rest of them....
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

我想做什么

我正在努力获得完全相同的输出,如上所示(所以foreach @insert_words先于,替换并接替网址中的每个查询参数),但我想替换复杂的正则表达式方法有一个更简单,更容易理解的方法,但我不知道最好的方法。

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

1 个答案:

答案 0 :(得分:3)

URI如何处理查询的文档中对此进行了描述。 URI::QueryParam模块提供允许与查询交互的query_param子例程。

use strict;
use warnings;
use URI;
use URI::QueryParam;

my @words = qw(HELLO GOODBYE);
my $URL = <DATA>;
my $uri = URI->new($URL);

for my $key ($uri->query_param) {                    # the keys of the query
    my $org = $uri->query_param($key);               # keep original value
    for my $word (@words) {
        for ("$org$word", $word, "$word$org") {   
            $uri->query_param($key, $_);             # set new value
            print $uri->as_string, $/;               # print new uri
        }
    }
    $uri->query_param($key, $org);                   # restore original value
}

__DATA__
http://www.example.com/index.php?route=9&other=7

<强>输出:

http://www.example.com/index.php?route=9HELLO&other=7
http://www.example.com/index.php?route=HELLO&other=7
http://www.example.com/index.php?route=HELLO9&other=7
http://www.example.com/index.php?route=9GOODBYE&other=7
http://www.example.com/index.php?route=GOODBYE&other=7
http://www.example.com/index.php?route=GOODBYE9&other=7
http://www.example.com/index.php?route=9&other=7HELLO
http://www.example.com/index.php?route=9&other=HELLO
http://www.example.com/index.php?route=9&other=HELLO7
http://www.example.com/index.php?route=9&other=7GOODBYE
http://www.example.com/index.php?route=9&other=GOODBYE
http://www.example.com/index.php?route=9&other=GOODBYE7