如何获取字符串指定索引处单词?
我的代码在遇到字符串首尾单词时会失效, 请问怎么解决?
# -*- coding: utf-8; -*-
from re import search;
def func(text: str, start: int) -> str:
end = start;
while not search(r"^.{1}\b", text[start-2:start]):
start -= 1;
while not search(r"\b.{1}$", text[end-1:end+1]):
end += 1;
return text[start-1: end]