我的代码在遇到字符串首尾单词时会失效, 请问怎么解决?
# -*- 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]
只能加个判断了,如果start小于2的话,start等于2之类的,可能还要定义第三个变量。