正则表达式:字符串中的单词索引

regex: word index in string

给定一个大字符串:

str = "Include all the information someone would need to answer your question ... ..."

如何仅通过 python 代码和正则表达式(不是列表等)来获取此字符串中整个单词的索引;这个索引是这个词的索引,如果它是列表中的一个元素:

lst = str.split() 

其中:“信息”一词的索引为3,“答案”的索引为8。

不确定您为什么要以这种方式 return 您的 Index,但也许是这样的:

Indx = re.findall(r'^.*\binformation\b', str)[0].count(' ')

或:

Indx = re.search(r'^.*\binformation\b', str).group(0).count(' ')

这会return字信息的position/index。