python - 如何分隔'di'单词中的前缀?
问题描述
我想在单词“di”后面跟字母之后分离一些集成到单词中的前缀。
sentence1 = "dipermudah diperlancar"
sentence2 = "di permudah di perlancar"
我期望这样的输出:
output1 = "di permudah di perlancar"
output2 = "di permudah di perlancar"
解决方案
这个表达式可能在某种程度上起作用:
(di)(\S+)
如果我们的数据看起来像问题中那样简单。否则,我们会在表达式中添加更多边界。
测试
import re
regex = r"(di)(\S+)"
test_str = "dipermudah diperlancar"
subst = "\\1 \\2"
print(re.sub(regex, subst, test_str))
该表达式在regex101.com的右上角面板上进行了说明,如果您希望探索/简化/修改它,并且在此链接中,您可以查看它如何与一些示例输入匹配,如果您愿意的话。
推荐阅读
- scala - 找不到参数写入错误的隐式值,但我使用宏定义了处理程序
- java - 使用 RestAssure 和 WireMock 实现验收测试
- python - 根据数据框中的其他行值创建列
- aws-fargate - 我们如何在 AWS 中部署 Backstage 应用程序?部署架构工作流程是什么?
- c - C 结构与 Fortran 的互操作
- sympy - 减少 sympy 中的不等式系统
- javascript - 如何使用 javascript 或 css 根据屏幕大小呈现 div
- html - 如何扩展导航栏背景,同时保持元素居中?
- python - 将 2 个具有不同列数的 Pandas 数据框相乘
- security - 使用 hcsshim 将 HNS 端点上的流量限制到特定 IP 地址