在字符串中解析url

时间:2011-09-30 21:08:54

标签: regex

我将如何匹配

遵循字符串格式(等号到.html结尾后的所有内容

http%3A%2F%2Fwww.mydomains.com.com%2FSA100.html

在下面的字符串中:

http://www.tticker.com/me0439-119?url=http%3A%2F%2Fwww.mydomains.com.com%2FSA100.html%3Fc%2acn%2CSA400

3 个答案:

答案 0 :(得分:0)

最简单的我能想到的是:

/url=(http.*\.html)/

使用您的网址捕获组。

答案 1 :(得分:0)

(?<==).*?\.html

使用grep进行测试

   kent$  echo "http://www.tticker.com/me0439-119?url=http%3A%2F%2Fwww.mydomains.com.com%2FSA100.html%3Fc%2acn%2CSA400"|grep -Po "(?<==).*?\.html"
    http%3A%2F%2Fwww.mydomains.com.com%2FSA100.html

答案 2 :(得分:0)

在perl:

#!/usr/bin/perl -w
use URI;
my $uri = URI->new("http://www.tticker.com/me0439-119?url=http%3A%2F%2Fwww.mydomains.com.com%2FSA100.html%3Fc%2acn%2CSA400"); # create URI object
my %params = $uri->query_form();                      # get all params
my $param_url = $params{url};

my $uri2 = URI->new($param_url); # create new URI object from param URL  
$uri2->query(undef);             # strip parameters
print $uri2->as_string();

给出:

http://www.mydomains.com.com/SA100.html