title: Regex - get URL protocol, host, path, but not filename - PCRE
body: <h1>Goal</h1>
  
  <p>Replace hosts and paths (locations), but keep filenames (they're unchanged).</p>
  
  <h1>URL without subdomain - does not work</h1>
  
  <p>This works for host (domains) that are have at least one subdomain (e.g. 'www.somedomain.com'), but fails to get the path with just the domain + TLD (e.g. 'somedomain.com')</p>
  
  <p><code>(http[s]?:\/\/([^:\/\s]+)(\/\w+)*\/)+</code></p>
  
  <p>In the following HTML snippet</p>
  
  <pre><code>junk before tag &lt;img src="https://somedomain.com/wp-content/uploads/2017/10/someimage.jpg" alt="" /&gt;Random text after
  </code></pre>
  
  <p>A PCRE engine will only capture:</p>
  
  <pre><code>https://somedomain.com/
  </code></pre>
  
  <h1>URL <em>with</em> subdomain - works</h1>
  
  <p>In the following HTML snippet (domain has a subdomain)</p>
  
  <pre><code>junk before tag &lt;img src="https://www.somedomain.com/wp-content/uploads/2017/10/someimage.jpg" alt="" /&gt;Random text after
  </code></pre>
  
  <p>A PCRE engine captures the entire URL (save for the file):</p>
  
  <pre><code>https://www.somedomain.com/wp-content/uploads/2017/10/
  </code></pre>
  
  <h1>Question</h1>
  
  <p>How can I adjust the regex to capture the full protocol, domain <strong><em>and path</em></strong> (but not filename) for <code>img src=""</code> URLs that have subdomains <strong><em>as well as</em></strong> those <em>without</em> subdomains?</p>