How to cover all case of <strong> <em> <u>
to <strong>
with python and smart way?
I was trying code to cover that but I think my way not good and have a lot of case.
Have any want know how to do that with better?
I very want have 2 way to learn that:
- Way 1: Not use regex.
- Way 2: Use regex.
My code now:
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)
Thanks for your support! All best with you. I very try to research more but not see case like that!
CodePudding user response:
What about using a simple list and a for loop?
def function_cover_all_strong(html):
#convert <strong class"some-thing" id="something"> to <strong>
while '<strong class' in html:
i = html.find('<strong class') #find start index
j = html[i:].find('>') #find end index
html = html[:i] '<strong>' html[j:] #replace
#convert tags
tags = ['<u><em><strong>', '</strong></em></u>', '<em><strong><u>']
for old_tag in tags:
if '/' in tag:
new_tag = '</strong>'
else:
new_tag = '<strong>'
html = html.replace(old_tag, new_tag)
return html