I like the answer of the URI. The OP requested a regular expression, so in honor of the request and as a challenge, here is the answer I came up with. In fairness, sometimes installing CPAN modules is not easy or possible. I worked on some projects that are hardened using a very specific version of Perl, and only certain modules are allowed.
Here is my attempt at a regular expression response. Please note that www. not required. Subdomains such as mobile. are performed. Search / not greedy, so the URL with directories at the end will be parsed correctly. I am protocol independent; it can be http, https, file, sftp . The output is recorded at $1 .
^.*://(?:[wW]{3}\.)?([^:/]*).*$
Input Example:
http://WWW.facebook.com:80/ http://facebook.com/xxxxxxxxxxx/aaaaa http://www.stackoverflow.com/yyyyyyyyyyyyyyyy/aaaaaaa https://mobile.yahoo.com/yyyyyyyyyyyyyyyy/aaaaaaa http://www.theregister.co.uk/
Output Example:
facebook.com facebook.com stackoverflow.com mobile.yahoo.com theregister.co.uk
EDIT: Thanks @ikegami for the extra task. :) Now it supports WWW in any mixed case and port number, for example :80 .
source share