Web Hosting Talk







View Full Version : Perl regular expression help


dynamicnet
12-16-2005, 05:08 PM
reetings:

The following perl code works to get just the machine name portion of a URL

## Get just the machine name or just the URL
## http://www.networksolutions.com/cgi-bin/whois/whois turned into www.networksolutions.com

## Expression will fail if http or https is not a part of the url.
$url =~ m#^http://(.*?)($|/)#i; ## Results go into $1
$url =~ m#^https://(.*?)($|/)#i; ## Results go into $1

It fails when the URL contains a port such as

http://cp.domain.com:8080/whatever

and

https://cp.domain.com:8443/whatever

Are there any regular expression experts present who can help me with modifying the above so that it works whether or not a port is included as part of the URL?

Thank you.

Burhan
12-17-2005, 02:54 AM
God I only did Perl when in college for a class, but try this:


$url =~ m|(\w+)://([^/:]+)(:\d+)?/(.*)|; # use m|...| so that we do not need to use a lot of "\/"
$protocol = $1;
$domainName = $2;
$uri = "/" . $4;
if ($3 =~ /:(\d+)/) { $portNo = $1}

dynamicnet
12-17-2005, 12:22 PM
Greetings:

God is very good.

Your perl code tested out very well.

Thank you very much for your help.