【发布时间】:2021-12-29 12:03:43
【问题描述】:
如何用python和智能方式覆盖<strong> <em> <u>到<strong>的所有情况?
我试图用代码来覆盖它,但我认为我的方法不好并且有很多案例。
有没有人想知道如何更好地做到这一点?
我非常希望有两种方法来学习:
- 方式 1:不使用正则表达式。
- 方式 2:使用正则表达式。
我现在的代码:
html = '''
<h1>Make bakery</h1>
<p>Step 1: Please use good quality product for make <strong><em>bakery</em></strong></p>
<p>Step 2: Make <em><u>bakery</u></em> hot</p>
<p>Step 2: Make <em><strong><u>bakery</u></strong></em> hot</p>
'''
def function_cover_all_strong(html):
html = html.replace('<u><em><strong>','<strong>')
html = html.replace('</strong></em></u>','</strong>')
html = html.replace('<em><strong><u>','<strong>')
html = html.replace('</u></strong></em>','</strong>')
html = html.replace('<strong><u><em>','<strong>')
html = html.replace('</em></u></strong>','</strong>')
html = html.replace('<strong><em><u>','<strong>')
html = html.replace('</u></em></strong>','</strong>')
html = html.replace('<em><u>','<strong>')
html = html.replace('</u></em>','</strong>')
html = html.replace('<u><strong>','<strong>')
html = html.replace('</strong></u>','</strong>')
html = html.replace('<u><em>','<strong>')
html = html.replace('</em></u>','</strong>')
html = html.replace('<strong><u>','<strong>')
html = html.replace('</u></strong>','</strong>')
html = html.replace('<strong><em>','<strong>')
html = html.replace('</em></strong>','</strong>')
html = html.replace('<u>','<strong>')
html = html.replace('</u>','</strong>')
html = html.replace('<em>','<strong>')
html = html.replace('</em>','</strong>')
return html
html_cover = function_cover_all_strong(html = html)
print(html_cover)
感谢您的支持!祝你一切顺利。我非常努力研究更多但没有看到这样的案例!
【问题讨论】:
-
非常感谢@Thomas 我会读到的
标签: python html string replace coding-style