重词法:词条索引

问题描述 投票:0回答:1

给定一个大字符串。

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

如何只用python代码和regex(不是列表之类的)来获取这个字符串中整个单词的索引;如果这个单词是列表中的一个元素,那么这个索引就是它的索引。

lst = str.split() 

其中:"信息 "一词的索引是3,"答案 "的索引是8... ...

regex indexing text substring word
1个回答
0
投票

不知道为什么你要返回你的 Index 这样,但也许是这样的。

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

or:

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

这将返回单词信息的位置索引。

© www.soinside.com 2019 - 2024. All rights reserved.