Remove Part of String Before the Last Forward Slash

巧了我就是萌 提交于 2020-02-18 05:02:20

问题


The program I am currently working on retrieves URLs from a website and puts them into a list. What I want to get is the last section of the URL.

So, if the first element in my list of URLs is "https://docs.python.org/3.4/tutorial/interpreter.html" I would want to remove everything before "interpreter.html".

Is there a function, library, or regex I could use to make this happen? I've looked at other Stack Overflow posts but the solutions don't seem to work.

These are two of my several attempts:

for link in link_list:
   file_names.append(link.replace('/[^/]*$',''))
print(file_names)

&

for link in link_list:
   file_names.append(link.rpartition('//')[-1])
print(file_names)

回答1:


Have a look at str.rsplit.

>>> s = 'https://docs.python.org/3.4/tutorial/interpreter.html'
>>> s.rsplit('/',1)
['https://docs.python.org/3.4/tutorial', 'interpreter.html']
>>> s.rsplit('/',1)[1]
'interpreter.html'

And to use RegEx

>>> re.search(r'(.*)/(.*)',s).group(2)
'interpreter.html'

Then match the 2nd group which lies between the last / and the end of String. This is a greedy usage of the greedy technique in RegEx.

Debuggex Demo

Small Note - The problem with link.rpartition('//')[-1] in your code is that you are trying to match // and not /. So remove the extra / as in link.rpartition('/')[-1].




回答2:


That doesn't need regex.

import os

for link in link_list:
    file_names.append(os.path.basename(link))



回答3:


You can use rpartition():

>>> s = 'https://docs.python.org/3.4/tutorial/interpreter.html'
>>> s.rpartition('/')
('https://docs.python.org/3.4/tutorial', '/', 'interpreter.html')

And take the last part of the 3 element tuple that is returned:

>>> s.rpartition('/')[2]
'interpreter.html'



回答4:


Just use string.split:

url = "/some/url/with/a/file.html"

print url.split("/")[-1]

# Result should be "file.html"

split gives you an array of strings that were separated by "/". The [-1] gives you the last element in the array, which is what you want.




回答5:


This should work if you plan to use regex

 for link in link_list:
    file_names.append(link.replace('.*/',''))
 print(file_names)



回答6:


Here's a more general, regex way of doing this:

    re.sub(r'^.+/([^/]+)$', r'\1', "http://test.org/3/files/interpreter.html")
    'interpreter.html'


来源:https://stackoverflow.com/questions/29657384/remove-part-of-string-before-the-last-forward-slash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!