首页 > 解决方案 > 删除所有跟在 4 个或更多字符后面的 -es/-s/-e/-x 后缀

问题描述

我正在尝试使用 Python 中的正则表达式删除所有单词后缀-es,或删除后缀-s后至少有 4 个字符的所有单词。-e-x

有一些期望输出的例子(法语)


我试图实现如下所示,但我发现它不是很有效。

def _stem_reg(word):
    pattern = "(\w{4,})(es$)|(\w{4,})(s$)|(\w{4,})(e$)|(\w{4,})(x$)"
    found = re.match(pattern, word)

    if found is not None:
        return next(group for group in found.groups() if group is not None)
    else:
        return word

标签: pythonregex

解决方案


尝试这个:^(\w{4,}?)(?:es|s|e|x)$

word = "feuilletées"
output = re.sub(r"^(\w{4,}?)(?:es|s|e|x)$", r'\1', word)
  • (\w{4,}?)捕获组 1 将匹配 4 个或更多字母。
  • (?:es|s|e|x)非捕获组将匹配范围内的所有后缀(-es、-s、-e、-x)

推荐阅读