Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 正则表达式:字符串中的单词索引_Python_Regex_Indexing_Substring_Word - Fatal编程技术网

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

Python 正则表达式:字符串中的单词索引,python,regex,indexing,substring,word,Python,Regex,Indexing,Substring,Word,给出一个大字符串: str = "Include all the information someone would need to answer your question ... ..." 如何仅通过python代码和正则表达式(而不是列表等)获得该字符串中整个单词的索引;如果该单词是列表中的一个元素,则该索引是该单词的索引: lst = str.split() 其中:“信息”一词的索引为3,“答案”一词的索引为8。不确定您为什么希望以这种方式返回索引,但可能类似于

给出一个大字符串:

str = "Include all the information someone would need to answer your question ... ..."
如何仅通过python代码和正则表达式(而不是列表等)获得该字符串中整个单词的索引;如果该单词是列表中的一个元素,则该索引是该单词的索引:

lst = str.split() 

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

不确定您为什么希望以这种方式返回
索引,但可能类似于:

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


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

您能更详细地解释这个问题吗。您的意思是
str.split().index('information')
?为什么要用
re
模块重新设计车轮?如果我需要所有位置怎么办?
Indx = re.search(r'^.*\binformation\b', str).group(0).count(' ')