如何在Python中使用re.sub?

Ahm*_*hin 2 python regex string regex-group regex-greedy

Text = "<a> text </a> <c> code </c>"                                               
Run Code Online (Sandbox Code Playgroud)

我想删除<c> code </c> python 中的语句

output = "<a> text </a>"
Run Code Online (Sandbox Code Playgroud)

sha*_*678 5

您可以使用re.sub

>>> import re
>>> text = "<a> text </a> <c> code </c>"
>>> new_text = re.sub(r'<c>.*?</c>', '', text)
>>> new_text
<a> text </a> 
Run Code Online (Sandbox Code Playgroud)