Python正则表达式可在变量中查找“文档”词

问题描述 投票:1回答:2

如果字符串词中包含文本“文档”,则需要将其替换为空字符串并保留结果文本。

请注意,如果字符串在向前方向上包含“文档”字母,则所有“文档”字母都需要用空字符串替换

应用示例

  1. doconeument转换为one
  2. documdocumentent转换为empty string
  3. documentone转换为one
  4. pydocdbument转换为pydb
  5. documentdocument转换为empty string
python regex
2个回答
6
投票

我有一个带有正则表达式和递归的解决方案:

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)}'")

编辑:对代码进行了更正(函数的第一行两行留在外面)。


0
投票

我知道您已经明确声明需要一个正则表达式,但是如果您不介意不使用正则表达式,可以尝试使用上述方法:

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'
© www.soinside.com 2019 - 2024. All rights reserved.