I have a BeautifulSoup Paragraph as string. I want to replace the occurrences of p (opening) and /p (closing) tags in the string using regular expressions because there are instances like
<p class="section-para">We would be happy to hear from you, Please
fill in the form below or mail us your requirements on<br/><span
class="text-red">contact@xyz.com</span></p>
But I can not use the generic
^< *>$
because I want strong,b and h1,h1..h6 tags for different purposes.
I only know basics of RegEx but do not know how to make and use one. Can somebody help me with the making "inclusion","exclusion" (if there is any). How can I make one for this problem and how can I substitute with the simple ''
def formatting(string):
this=['<h1>','</h1>','<h2>','</h2>','<h3>','</h3>','<h4>','</h4>','<h5>','</h5>','<h6>','</h6>','<b>','</b>','<strong>','</strong>']
with_this=['\nh1 Tag:','\n','\nh2 Tag:','\n''\nh3 Tag:','\n''\nh4 Tag:','\n''\nh5 Tag:','\n''\nh6 Tag:','\n','\Bold:','\n''\nBold:','\n']
for i in range(len(this)):
if this[i] in string:
string=string.replace(this[i],with_this[i])
return(string)
I have used replace functions of strings for h1,2...6 tags. Any help would be appreciated.
tag might actually be possible because you can't nest a
inside another
(or at least, you're not supposed to).
– Kevin Jun 20 '19 at 13:06tags and nothing else, that's possible. But it looks like you also want to replace and . Those tags _can_ be nested, so you can't write a regex that matches them.
– Kevin Jun 20 '19 at 13:14with?
– Chris Doyle Jun 20 '19 at 13:26