Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Can someone give me a regex to match a URL? www.domain.extension? I want www, the domain, and the extension. m/(www)\.({a-zA-Z})\.(...)/ was the best I could come up with. The a-zA-Z surely isn't good enough because domains can use - and other characters. Any help would be appreciated.

Replies are listed 'Best First'.
Re: regex of a url
by Abigail-II (Bishop) on Feb 26, 2004 at 02:10 UTC
    use Regexp::Common; /$RE{URI}/;

    Abigail

Re: regex of a url
by ysth (Canon) on Feb 26, 2004 at 02:24 UTC
    Despite asking for URL matching, I think you are looking for a domain name matcher. Try something like:
    use Regexp::Common; if ($name =~ /^$RE{net}{domain}\z/) { ... }
    Note that there is no requirement for a webserver to be on a domain starting with www.
      Note that /^$RE{net}{domain}\z/ will match a string consisting of a single space (it does so because RFC 1035 says that's a valid domain). Use
      /^(?! )$RE{net}{domain}\z/ # Or /^$RE{net}{domain}{-nospace}\z/
      if you don't want to match single space strings.

      Abigail