【问题标题】:PYTHON REGEXP to replace recognized pattern with the pattern itself and the replacement?PYTHON REGEX 用模式本身和替换替换识别模式?
【发布时间】:2012-03-05 22:37:33
【问题描述】:

文字-

.1。这真是太棒了。2。谷歌刚刚毁了 Apple.3。苹果毁了自己! 模式=(点)(数字)(点)(单个空格)

假设你有 30 到 40 个句子,上面的模式中带有段落编号。 <p> 标签应该被替换在段落编号后面! 使用re.sub()

我希望文本是:

</p> <p style="text-align: justify;">1. This is just awesome.</p> <p style="text-align: justify;">2. Google just ruined Apple.</p> <p style="text-align: justify;">3. Apple ruined itself!

【问题讨论】:

标签: python regex matching


【解决方案1】:

这就是匹配组在正则表达式中的用途。

你想要的是这样的:

new_string = re.sub(r'\.(\d+\. )', '</p><p style="text-align: justify;">\\1', old_string)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多