简体   繁体   English

使用Python从后搜索子字符串

[英]search sub-string from backward using Python

Wondering if there is a neat API in Python to search a string from backward (from a specific position in a string), for example, in string "Hello StackOverflow Hello Python", suppose I want to find the index of sub-string "Hello" which is previous to sub-string "Python"? 想知道Python中是否有一个整齐的API,可以从后向(从字符串中的特定位置)搜索字符串,例如,在字符串“ Hello StackOverflow Hello Python”中,假设我要查找子字符串“ Hello”的索引子字符串“ Python”之前的哪个?

thanks in advance, Lin 预先感谢林

Yes, it's called str.rindex() . 是的,它叫做str.rindex() It takes arguments sub - a substring to look for, start - a starting position, and end . 它需要参数sub (要查找的子字符串), start (起始位置)和end

It is the r everse of the index function. 它是r中的EVERSE index功能。 There are some others, like rsplit , too. 还有其他一些,例如rsplit

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

相关问题 当我们有多个子字符串时,如何使用python替换字符串的特定子字符串? - How to replace specific sub-string of a string using python while we have multiple sub-string? 如何使用python在xml文件的XPATH中搜索部分子字符串或正则表达式 - how to search for partial sub-string or regex in XPATH in a xml file using python Python:通过嵌入复杂算法来改善子字符串搜索 - Python: Improving sub-string search by embedding sophisticated algorithms 从另一个列表中搜索任何子字符串的字符串列表 - Search a list of strings for any sub-string from another list 从python中的字符串中获取特定的子字符串 - Take a specific sub-string from a string in python 逗号后从python字符串中删除子字符串 - Remove sub-string from python string after comma 使用正则表达式将文件中的子字符串替换为 python - replace sub-string in a file using regex with python 使用正则表达式向后搜索字符串(在Python中)? - Search backward through a string using a regex (in Python)? 使用正则表达式从文档中的给定字段中删除子字符串,并在 mongo 中在其前面添加另一个子字符串 - Using regex to remove sub-string from a given field in a document and adding another sub-string in front of it in mongo 从字符串的开头删除子字符串 - Remove sub-string from beginning of string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM