title: How to extract hostname and port from URL string?
body: <p>I need to extract hostname and port (if any) from an URL and here below is my code:</p>
  
  <pre><code>val regEx = """^(?:https?:\/\/)?(?:www\.)?(.*?)\//""".r
  val url = regEx.split("http://www.domain.com:8080/one/two")
  val hostname = url(url.length - 1).split("/")(0).split(":")(0)
  val hasPort = url(url.length - 1).split("/")(0).split(":").length &gt; 1
  val port = if (hasPort) url(url.length - 1).split("/")(0).split(":")(1) else 80
  </code></pre>
  
  <p>The code above works as expected... but for sure in Scala there is a better way to get the same result.</p>
  
  <p>How do I get hostname and port (if any) without using all those ugly <code>split</code>s?</p>