
vikas.deep
User
May 2, 2009, 1:57 AM
Post #4 of 7
(11109 views)
|
Re: [prateekm21] Regular expression to extract domain name
[In reply to]
|
Can't Post
|
|
Hi Prateekm, Hope you must have been benefited by other replies to your query. I was just going through the various responses to your query I tried the one suggested by one of our friends It works well for first eg in the query but fails in case of second query. It won't even compile if you "use strict". I can think of only two options First do not "use strict" during compilation. Second escape the $ symbols in the string $url. Above all are you absolutely sure that you have spotted such a url which carries $symbols. The url you have mentioned took me to some USA.com In that site I tried a few links like "hotel reservation", "Car rental", "Airline reservation" but at least in these three cases I could not spot a $ in the url address. You see the $ is a reserved symbol in PERL and that is the reason that $ is not acceptable in the variable $url during compilation. Also I may add that if you are working on a large number of cases(URLs) then the ".com" loses its significance In that case a better approach will be to combine the two suggestions made by our friends i.e. first split at "\" and then again explode it into an array at the dot character. Now you can just discard the "com". In my example code I have escaped all the $. There might me a better solution because of "TMTOWDI" #!/usr/bin/perl use strict; use warnings; my $url = "http://test.usa.com/o/tA\$IXTq\$2ALoYduAJQs\$j.AJQRO-QA/nourl1"; my @arr = split(/\//,$url); $arr[2] =~ s/^www.//; my @arr2 = split(/\./,$arr[2]); print "@arr2"; print "\n",$arr[2]; -For all my suggestions " I am sure someone else can do it in a better or elegant manner!"
(This post was edited by vikas.deep on May 2, 2009, 2:03 AM)
|