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(' ')