python - 如何分隔 'di' 单词中的前缀?

标签 python regex python-3.x string string-matching

我想在“di”后面跟字母后分离出一些集成到单词中的前缀。

sentence1 = "dipermudah diperlancar"
sentence2 = "di permudah di perlancar"

我期望这样的输出:

output1 = "di permudah di perlancar"
output2 = "di permudah di perlancar"

Demo

最佳答案

这个表达式在某种程度上可能有效:

(di)(\S+)

如果我们的数据看起来像问题中一样简单。否则,我们会在表达式中添加更多边界。

测试

import re    
regex = r"(di)(\S+)"    
test_str = "dipermudah diperlancar"    
subst = "\\1 \\2"    

print(re.sub(regex, subst, test_str))

表达式在 regex101.com 的右上面板中进行了解释, 如果你想探索/简化/修改它,在this link ,如果愿意,您可以观察它如何与一些样本输入相匹配。

关于python - 如何分隔 'di' 单词中的前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283223/

相关文章:

javascript - 如何识别正则表达式

python - 在 Python 3.2+ 中添加到 locals()?

javascript - 这个示例 CKEditor javascript 函数有什么作用?

python - SQLAlchemy ORM - 延迟约束检查

python - 使用 turtle 模块 exitonclick()

python - 如何在 Pandas 中添加字符串数字

python - 可扩展项目的 graphql 代码组织

python - 将文本文件导入 Python/Pandas 时出现问题

python - 让 Tornado 使用 javascript 提供静态 HTML,而不使用静态、公共(public)等前缀

regex - 你如何用 sed "debug"一个正则表达式?