gangi gangi - 3 months ago 10
Python Question

Regex to parse out a part of URL

I am having the following data,

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/


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,

parsed
a-b-n-r-y-u
best-food-for-humans
a-w-w-2.jpg
NULL
NULL
NULL


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.

Thanks

Answer

Another option is to simply split on "/" and take the last element:

"http://hsotname.com/2016/08/a-b-n-r-y-u".split("/")[-1]
# 'a-b-n-r-y-u'

"http://www.hostname.com/a/geniusbar/".split("/")[-1]
# ''