如果字符串词中包含文本“文档”,则需要将其替换为空字符串并保留结果文本。
请注意,如果字符串在向前方向上包含“文档”字母,则所有“文档”字母都需要用空字符串替换
应用示例
doconeument
转换为one
documdocumentent
转换为empty string
documentone
转换为one
pydocdbument
转换为pydb
documentdocument
转换为empty string
我有一个带有正则表达式和递归的解决方案:
from re import compile
candidates = ["doconeument", "documdocumentent", "documentone",
"pydocdbument", "documentdocument", "hansi"]
word = "document"
def strip_word(word, candidate):
regex = compile("^(.*)" + "(.*)".join(word) + "(.*)$")
match = regex.match(candidate)
if not match:
return candidate
return strip_word(word, "".join(match.groups()))
for cand in candidates:
print(f"'{cand}' -> '{strip_word(word, cand)}'")
编辑:对代码进行了更正(函数的第一行两行留在外面)。
我知道您已经明确声明需要一个正则表达式,但是如果您不介意不使用正则表达式,可以尝试使用上述方法:
def clear_word(s, word="document"):
if len(s) >= len(word):
i = 0
res_list = list()
for char in s:
try:
if char == word[i]:
i += 1
continue
except IndexError:
pass
res_list.append(char)
res = "".join(res_list)
return res if not word in res else clear_word(res, word)
else:
return s
您将使用它为:
>>> clear_word("pydocdbument")
'pydb'