2013-03-12 52 views
-1

我有以下代碼:Perl的更換複雜的正則表達式與簡單的方法

#!/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先於,替換併成功在URL中的每個查詢參數),但我想用一個更簡單,更容易理解的方法替換複雜的正則表達式方法,但我不知道最好的解決方法。

您對我們的幫助將非常感激,非常感謝

+1

我相信這是你問這個問題的第五個問題,我不知道你實際上自己在這個過程中編寫任何代碼的任何證據。如果你想要別人這樣做,你需要僱用一個人。如果你想學習Perl,你需要從基礎知識開始,然後真正弄清它是如何工作的。 – 2013-03-12 14:36:27

+0

@ dan1111 - 我很欣賞你從哪裏來,很多問題的原因是由於我的需求不斷變化,以及實現它的新方法,比如URI :: QueryParam被顯示給我,我不知道,謝謝 – 2013-03-12 14:50:13

+0

@ perl-user,我的觀點是你的過程似乎不涉及任何實際的*學習*。當你得到一個新的要求時,你不會嘗試任何東西,只要回到現場並再次尋求解決方案。很多人都很樂意提供幫助,但希望你的目標是成爲一名更好的程序員。如果你想這樣做,你需要更多的自己來解決問題。 – 2013-03-12 14:53:13

回答

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