如何在python中向后搜索?

时间:2013-07-30 06:06:12

标签: python html

我正在处理包含第1项,第1a项和第2项的html文件。有没有办法向后搜索并找到第2项之前的项目1a?

示例:

text= """ this is an example item 1a thanks for helping item 2 blah blah item 1a""" 

2 个答案:

答案 0 :(得分:4)

使用str.rfind()方法以反方向搜索字符串的出现。 This问题还应该为您提供一些有关如何实现的见解

答案 1 :(得分:0)

是的...使用'r'功能:

text= """ this is an example item 1a thanks for helping item 2 blah blah item 1a"""
text.rfind("1a")