查找字符后的最后一个子字符串

时间:2022-03-23 00:24:45

I know many ways how to find a substring: from start index to end index, between characters etc., but I have a problem which I don't know how to solve: I have a string like for example a path: folder1/folder2/folder3/new_folder/image.jpg and the second path: folder1/folder2/folder3/folder4/image2.png

我知道很多如何找到子字符串的方法:从开始索引到结束索引,在字符之间等等,但是我有一个问题,我不知道如何解决:我有一个字符串,例如一个路径:folder1/folder2/folder3/new_folder/image.jpg和第二个路径:folder1/folder2/folder3/folder4/image2.png。

And from this paths I want to take only the last parts: image.jpg and image2.png. How can I take a substring if I don't know when it starts (I don't know the index, but I can suppose that it will be after last / character), if many times one character repeats (/) and the extensions are different (.jpg and .png and even other)?

在这条路径中,我只需要最后一个部分:image.jpg和image2.png。如果我不知道它什么时候开始(我不知道索引,但是我可以假设它将在最后/字符之后),如果很多时候一个字符重复(/)和扩展是不同的(.jpg和.png,甚至是其他的),那么我该如何获取子字符串呢?

4 个解决方案

#1


15  

Use os.path.basename() instead and not worry about the details.

使用os.path.basename(),不要担心细节。

os.path.basename() returns the filename portion of your path:

basename()返回路径的文件名部分:

>>> import os.path
>>> os.path.basename('folder1/folder2/folder3/new_folder/image.jpg')
'image.jpg'

For a more generic string splitting problem, you can use str.rpartition() to split a string on a given character sequence counting from the end:

对于更一般的字符串分割问题,您可以使用str.rpartition()来在给定的字符序列中从结束处拆分一个字符串:

>>> 'foo:bar:baz'.rpartition(':')
('foo:bar', ':', 'baz')
>>> 'foo:bar:baz'.rpartition(':')[-1]
'baz'

and with str.rsplit() you can split multiple times up to a limit, again from the end:

使用str.rsplit(),您可以多次分割到一个极限,同样从末尾开始:

>>> 'foo:bar:baz:spam:eggs'.rsplit(':', 3)
['foo:bar', 'baz', 'spam', 'eggs']

Last but not least, you could use str.rfind() to find just the index of a substring, searching from the end:

最后,您可以使用string .rfind()只查找子字符串的索引,从末尾开始搜索:

>>> 'foo:bar:baz'.rfind(':')
7

#2


3  

You can do this as well -

你也可以这样做。

str_mine = 'folder1/folder2/folder3/new_folder/image.jpg'    
print str_mine.split('/')[-1]

>> image.png

#3


1  

      import re
      pattern=re.compile(r"(.*?)/([a-zA-Z0-9]+?\.\w+)")
      y=pattern.match(x).groups()
      print y[1]

No length constraints.

没有长度限制。

#4


1  

you can do this split on last occurrence

您可以在最后一次发生时执行此分割

my_string='folder1/folder2/folder3/new_folder/image2.png'
print(my_string.rsplit("/",1)[1])
output:-
image2.png

#1


15  

Use os.path.basename() instead and not worry about the details.

使用os.path.basename(),不要担心细节。

os.path.basename() returns the filename portion of your path:

basename()返回路径的文件名部分:

>>> import os.path
>>> os.path.basename('folder1/folder2/folder3/new_folder/image.jpg')
'image.jpg'

For a more generic string splitting problem, you can use str.rpartition() to split a string on a given character sequence counting from the end:

对于更一般的字符串分割问题,您可以使用str.rpartition()来在给定的字符序列中从结束处拆分一个字符串:

>>> 'foo:bar:baz'.rpartition(':')
('foo:bar', ':', 'baz')
>>> 'foo:bar:baz'.rpartition(':')[-1]
'baz'

and with str.rsplit() you can split multiple times up to a limit, again from the end:

使用str.rsplit(),您可以多次分割到一个极限,同样从末尾开始:

>>> 'foo:bar:baz:spam:eggs'.rsplit(':', 3)
['foo:bar', 'baz', 'spam', 'eggs']

Last but not least, you could use str.rfind() to find just the index of a substring, searching from the end:

最后,您可以使用string .rfind()只查找子字符串的索引,从末尾开始搜索:

>>> 'foo:bar:baz'.rfind(':')
7

#2


3  

You can do this as well -

你也可以这样做。

str_mine = 'folder1/folder2/folder3/new_folder/image.jpg'    
print str_mine.split('/')[-1]

>> image.png

#3


1  

      import re
      pattern=re.compile(r"(.*?)/([a-zA-Z0-9]+?\.\w+)")
      y=pattern.match(x).groups()
      print y[1]

No length constraints.

没有长度限制。

#4


1  

you can do this split on last occurrence

您可以在最后一次发生时执行此分割

my_string='folder1/folder2/folder3/new_folder/image2.png'
print(my_string.rsplit("/",1)[1])
output:-
image2.png