# 给定英文text,以及span,判断span是否在text,若在返回start /end index,从1开始计数,如果不在,则返回0,0
import re
text = "This is a tt sample text"
span = "sample text"
def get_index(text, span):
match = re.search(span, text)
if match == None:
index = (0,0)
else:
start_index = match.start()
end_index = match.end()
words_before_span = text[:start_index].split()
words = text.split()
start_word_index = len(words_before_span) + 1
end_word_index = start_word_index + len(span.split()) - 1
index =(start_word_index, end_word_index)
return index
start, end = get_index(text, span)
print(start, end)
Given English text and span, judge whether the span is in the text, if it returns start/end index, counting from 1, if not, return 0,0
Guess you like
Origin blog.csdn.net/weixin_41862755/article/details/130208417
Ranking