如何使用 replace 或 re.sub 正确删除所需的内容?

小弟需要从CSV档删除部分内容,内容是HTML代码,需要删除一些代码的内容。
以下是我的csv档案内容

林祖舜(Joe),5/3/2020 20:06,"<div class=""ContentGrid"">
之前有幾位熱心的巴打幫手係全球搵口罩,第二批貨終於返咗嚟,大概有400盒,20,000個口罩,之前嗰3張list 已經清晒,我哋會跟返之前大家落咗名嘅waiting list PM大家張口罩order form (價錢HK$90一盒(一盒50), 每人限購二盒),巴打們仍然會繼續搵緊更多嘅口罩俾大家,不過越黎越困難,價錢越嚟越貴。<img data-icons="":~("" src=""/faces/cry.gif"" alt="":~(""> <br><br><a href=""https://upload.cc/i1/2020/02/20/g7no03.jpeg"" target=""_blank""><img src=""https://upload.cc/i1/2020/02/20/g7no03.jpeg"" alt=""https://upload.cc/i1/2020/02/20/g7no03.jpeg"" data-src=""https://upload.cc/i1/2020/02/20/g7no03.jpeg"" onload=""DrawImage(this)"" width=""300"" height=""617""></a><br><br>之前大家落咗名嘅口罩訂購-Waiting List:<br><a href=""https://docs.google.com/forms/d/1gu33q5_XO3PqhwRm7rGLQrwNuScfDlvPvCCHRd81Fdk"" target=""_blank"">https://docs.google.com/forms/d/1gu33q5_XO3PqhwRm7rGLQrwNuScfDlvPvCCHRd81Fdk</a><br><br>無收到PM訂口罩都可以訂搓手液, 要填呢張form<br><a href=""https://forms.gle/YmQewsz1z689QtKx7"" target=""_blank"">https://forms.gle/YmQewsz1z689QtKx7</a><br><br>所有26/2之前嘅order 已經送出。我哋張會處理26/2或之後嘅order,感謝皇巴、菊巴、六仔同其他巴絲打無私的幫手! <img data-icons=""#good2#"" src=""/faces/ThumbUp.gif"" alt=""#good2#""> 希望大家一齊共渡難關,這裏是高登,這裏有力量! <br><br>高登有愛,共勉之。<img data-icons=""#hoho#"" src=""/faces/hoho.gif"" alt=""#hoho#""><br><br>#政府做唔到<img data-icons=""#ass#"" src=""/faces/ass.gif"" alt=""#ass#""><br>#高登仔做到<img data-icons=""#yup#"" src=""/faces/agree.gif"" alt=""#yup#"">
<br><br><br>
</div>"
大Hi清,5/3/2020 20:17,"<div class=""ContentGrid"">
<a href=""https://upload.cc/i1/2020/03/05/GvHKrm.png"" target=""_blank""><img src=""https://upload.cc/i1/2020/03/05/GvHKrm.png"" alt=""https://upload.cc/i1/2020/03/05/GvHKrm.png"" data-src=""https://upload.cc/i1/2020/03/05/GvHKrm.png"" onload=""DrawImage(this)"" width=""300"" height=""400""></a><br><br><a href=""https://upload.cc/i1/2020/03/05/WjYCvz.png"" target=""_blank""><img src=""https://upload.cc/i1/2020/03/05/WjYCvz.png"" alt=""https://upload.cc/i1/2020/03/05/WjYCvz.png"" data-src=""https://upload.cc/i1/2020/03/05/WjYCvz.png"" onload=""DrawImage(this)"" width=""236"" height=""350""></a>
<br><br><br>
</div>"

enter image description here
我使用了
我使用了 re.sub 删除部分的内容,但一直发生多删除内容。
以下程式码是一个例子:
new_html_code = re.sub('" target="_blank">.*</a>', " ", new_html_code)

我只想删除以下的内容:

" target="_blank"><img src="https://upload.cc/i1/2020/02/20/g7no03.jpeg" alt="https://upload.cc/i1/2020/02/20/g7no03.jpeg" data-src="https://upload.cc/i1/2020/02/20/g7no03.jpeg" onload="DrawImage(this)" width="300" height="617"></a>

但很不幸,我的程式码会多删除内容,结果变成删除以下的内容:

" target="_blank"><img src="https://upload.cc/i1/2020/02/20/g7no03.jpeg" alt="https://upload.cc/i1/2020/02/20/g7no03.jpeg" data-src="https://upload.cc/i1/2020/02/20/g7no03.jpeg" onload="DrawImage(this)" width="300" height="617"></a><br><br>之前大家落咗名嘅口罩訂購-Waiting List:<br><a href="https://docs.google.com/forms/d/1gu33q5_XO3PqhwRm7rGLQrwNuScfDlvPvCCHRd81Fdk" target="_blank">https://docs.google.com/forms/d/1gu33q5_XO3PqhwRm7rGLQrwNuScfDlvPvCCHRd81Fdk</a><br><br>無收到PM訂口罩都可以訂搓手液, 要填呢張form<br><a href="https://forms.gle/YmQewsz1z689QtKx7" target="_blank">https://forms.gle/YmQewsz1z689QtKx7</a>

每个栏位都有很多 </a> 的内容。我知道我的程式码是只能读取最后一个 </a>。我希望能让程式码读取第一个</a>,而不是最后一个 </a>

希望大家可以帮到我,谢谢。

最佳答案

用懒惰模式:

new_html_code = re.sub('" target="_blank">.*?</a>', " ", new_html_code, 1)

最后一个参数 1 表示只替换前一个匹配项。默认为0,意思是全部替换。

3年前 评论
fd5556 (楼主) 3年前
讨论数量: 1
Jason990420
  • 重复 (+, *) 是贪婪的, 后面多加一个 '?', 就会变成懒惰的
  • 贪婪在于会继续往后匹配, 懒惰就不会.
3年前 评论
fd5556 (楼主) 3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!