Dmy*_*nko 1 regex url perl replace
能否请你帮我用Perl的正则表达式替换(HTTP://.+)至http://www.my1.com/redir?$ 1,但什么也不干的URL像http://www.my1.com/或http://my1.com/
例如,我需要将http://whole.url.site.com/foo.htm替换 为http://www.my1.com/redir?http://whole.url.site.com/foo.htm http ://www.google.com到http://www.my1.com/redir?http://www.google.com 但请保留http://www.my1.com/index.php.
非常感谢!
如果您在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)
| 归档时间: |
|
| 查看次数: |
189 次 |
| 最近记录: |