本文介绍了PYTHON REGEXP会将识别的图案替换为图案本身并进行替换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文本-

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

.1. This is just awesome.2. Google just ruined Apple.3. Apple ruined itself!pattern = (dot)(number)(dot)(singlespace)

想象一下,在上面的模式中,您有30到40个句子的段落编号. <p>标签应替换在参数编号后面! 使用re.sub()

Imagine you have 30 to 40 sentences with paragraph numbers in the above pattern. A <p> tag should be replaced behind THE PARAGRAPH NUMBER! USING 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!

推荐答案

这是正则表达式中匹配组的作用.

This is what matching groups are for in regular expresssions.

您想要的是这样的

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

这篇关于PYTHON REGEXP会将识别的图案替换为图案本身并进行替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:33