2012-02-14 59 views
-2

文本-PYTHON REGEXP用模式本身和替換替換已識別的模式?

.1。這真是太棒了。谷歌剛剛毀了蘋果。蘋果毀了自己! pattern =(dot)(number)(dot)(singlespace)

想象一下,在上述模式中,您有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! 
+0

可能重複:http://stackoverflow.com/questions/5984633/python-re-sub-group-number-after-number – Marii 2012-02-14 15:01:28

回答

1

這就是匹配的組是在常規expresssions。

你需要的是這樣的:這

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