我想在“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 的右上面板中进行了解释, 如果你想探索/简化/修改它,在this link ,如果愿意,您可以观察它如何与一些样本输入相匹配。
关于python - 如何分隔 'di' 单词中的前缀?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57283223/