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>Regexes are probably not the way you should do this - considering that you marked the question <code>python</code>, try (assuming the URL is in name <code>url</code>):</p>

<pre><code>last-part = url.split('/')[-1]
</code></pre>

<p>This splits the URL into a list of substrings between slashes, and stores the last one in <code>last-part</code>.</p>

<p>If you insist on using regexes, though, matching on the end of the string is helpful here. Try <code>/[^/]*$</code>, which matches a slash, followed by any number of non-slashes, followed by the end of the string.</p>

<p>If you were to want to match the last non-empty part following a slash (if you didn't want the last three examples to return <code>""</code>), you could do <code>/[^/]*/?$</code>, which allows but does not require a single slash at the very end.</p>

python regex regex-negation