简体   繁体   English

如何在python中向后搜索?

[英]how to search backwards in python?

I am working with html files which have item 1, item 1a and item 2. is there a way to search backwards and find the item 1a which comes before an item 2? 我正在使用具有项目1,项目1a和项目2的html文件。是否有一种方法可以向后搜索并找到项目2之前的项目1a?

example: 例:

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

Use str.rfind() method to search for the occurence of a string in the reverse direction. 使用str.rfind()方法搜索反向出现的字符串。 This question should also give you some insight on how to go about 这个问题还应该使您对如何进行了解

yes... use the 'r' functions: 是的...使用“ r”功能:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM