title: Properly Matching a IDN URL
body: <p><strong>I need help building a regular expression that can properly match an URL inside free text.</strong></p>
  
  <ul>
  <li><strong>scheme</strong>
  
  <ul>
  <li>One of the following: <strong>ftp</strong>, <strong>http</strong>, <strong>https</strong> (is <strong>ftps</strong> a protocol?)</li>
  </ul></li>
  <li>optional <strong>user</strong> (and optional <strong>pass</strong>)</li>
  <li><strong>host</strong> (with support for IDNs)
  
  <ul>
  <li>support for <strong>www</strong> and <strong>sub-domain(s)</strong> (with support for IDNs)</li>
  <li>basic filtering of TLDs (<code>[a-zA-Z]{2,6}</code> is enough I think)</li>
  </ul></li>
  <li>optional <strong>port</strong> number</li>
  <li><strong>path</strong> (optional, with support for Unicode chars)</li>
  <li><strong>query</strong> (optional, with support for Unicode chars)</li>
  <li><strong>fragment</strong> (optional, with support for Unicode chars)</li>
  </ul>
  
  <p>Here is what I could find out about sub-domains:</p>
  
  <blockquote>
    <p>A "subdomain" expresses relative
    dependence, not absolute dependence:
    for example, wikipedia.org comprises a
    subdomain of the org domain, and
    en.wikipedia.org comprises a subdomain
    of the domain wikipedia.org. In
    theory, this subdivision can go down
    to 127 levels deep, and each DNS label
    can contain up to 63 characters, as
    long as the whole domain name does not
    exceed a total length of 255
    characters.</p>
  </blockquote>
  
  <p>Regarding the domain name itself I couldn't find any reliable source but I think the regular expression <strong>for non-IDNs</strong> (I'm not sure how to write a IDN compatible version) is something like:</p>
  
  <pre><code>[0-9a-zA-Z][0-9a-zA-Z\-]{2,62}
  </code></pre>
  
  <p><strong>Can someone help me out with this regular expression or point me to a good direction?</strong></p>