python - 删除所有跟在 4 个或更多字符后面的 -es/-s/-e/-x 后缀
问题描述
我正在尝试使用 Python 中的正则表达式删除所有单词后缀-es
,或删除后缀-s
后至少有 4 个字符的所有单词。-e
-x
有一些期望输出的例子(法语):
- 技术→技术
- pare s → pare (这个词太小所以它没有删除“es”,只删除“s”)
- bas → bas (这个词太小所以什么都做不了)
- 材料x → 材料
- 围攻→围攻
- 围攻→围攻
- feuilleté es → feuilleté
- 多斯→多斯
我试图实现如下所示,但我发现它不是很有效。
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
解决方案
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)
推荐阅读
- php - 如何从 db 获取上周和上个月的注册用户
- java - 推送通知前台和后台
- google-apps-script - 带有 4 个主题标签的 Gmail 一键式操作##Hash## 不起作用
- java - JpaRepository findOne 返回 null
- sql - SQL 字母数字选择
- swift - 如何将 Firebase 中的查询过滤到我的帖子子项中的特定字段?- Firebase 和 Swift
- azure-iot-edge - 从环境中派生设备连接字符串
- android - 在 Navigation Drawer Header 中拦截触摸事件以允许 RecyclerView 滚动?
- ios - 错误:在 IOS 项目(swift)中嵌入 Unity(Vuforia)时架构 arm64 的未定义符号
- excel - Excel 多个 If 语句显示 False