bri*_*foy 7

如果您在Perl脚本中执行此操作,请不要使用正则表达式.在这种情况下阅读它们是一团糟,到目前为止,每个正则表达式的答案都被打破了,因为它没有URI转义你要放入查询字符串的东西.

不要试图自己解析URI,而是让经过时间考验的URI模块为您处理所有边缘情况.该URI ::逃生模块可以帮助你做出的查询字符串,所以你不要被奇怪的字符在URL中轮回一圈:

#!perl

use URI;
use URI::Escape;

while( <DATA> )
    {
    chomp;

    my $url = URI->new( $_ );

    if( $url->host =~ /(^|\.)my1\.com$/ ) {
        print "$url\n";
        }
    else {
        my $query_string = uri_escape($url->as_string);
        print "http://www.my1.com/redir?$query_string\n";
        }
    }

__DATA__
http://whole.url.site.com/foo.htm
http://www.google.com
http://www.google.com/search?q=perl+uri
http://www.my1.com/index.php
http://my1.com/index.php
http://moremy1.com/index.php
Run Code Online (Sandbox Code Playgroud)