Regex to parse out a part of URL
<p>I am having the following data,</p>

<pre><code>data
http://hsotname.com/2016/08/a-b-n-r-y-u
https://www.hostname.com/best-food-for-humans
http://www.hostname.com/wp-content/uploads/2014/07/a-w-w-2.jpg
http://www.hostname.com/a/geniusbar/
http://www.hsotname.com/m/
http://www.hsotname.com/
</code></pre>

<p>I want to avoid the first http:// or https:// and check for the last '/' and parse out the remaining parts of the URL. But the challenge here is, we have '/' on the end of few URLs as well. The output which I want is,</p>

<pre><code>parsed
a-b-n-r-y-u
best-food-for-humans
a-w-w-2.jpg
NULL
NULL 
NULL
</code></pre>

<p>Can anybody help me to find the last / and parse out the remaining part of the URL? I am new to regex and any help would be appreciated.</p>

<p>Thanks</p>

<p>Possibly over kill for the example, but if you need to deal with location fragments/just location names (ie, the last forward slash is part of the http etc... (splitting <code>http://hostname.com</code> and taking the last <code>/</code> will give you <code>hostname.com</code> - <code>urlsplit</code> will give a path of <code>''</code> instead) then'll you're probably safer off using:</p>

<pre><code>&gt;&gt;&gt; from urllib.parse import urlsplit
&gt;&gt;&gt; urls = ['http://hsotname.com/2016/08/a-b-n-r-y-u', 'https://www.hostname.com/best-food-for-humans', 'http://www.hostname.com/wp-content/uploads/2014/07/a-w-w-2.jpg', 'http://www.hostname.com/a/geniusbar/', 'http://www.hsotname.com/m/', 'http://www.hsotname.com/']
&gt;&gt;&gt; [urlsplit(url).path.rpartition('/')[2] for url in urls]
['a-b-n-r-y-u', 'best-food-for-humans', 'a-w-w-2.jpg', '', '', '']
</code></pre>

python regex regex-negation