how to remove <BR> using replace function?

Duncan Booth duncan.booth at invalid.invalid
Thu Feb 9 03:45:42 EST 2006


Rinzwind wrote:

> Works for me.
> 
>>>> txt = "an unfortunate <br> in the middle"
>>>> print txt.replace("<br>", "")
> an unfortunate  in the middle
>>>> 
> 
> 
> Though I don't like the 2 spaces it gives ;)
> 
Although I generally advise against overuse of regular expressions, this is 
one situation where regular expressions might be useful: the situation is 
simple enough not to warrant a parser, but apart from the whitespace a <br> 
tag could have attributes or be written in xhtml style <br />. Also judging 
by the inconsistency between the OP's subject line and his original 
question he doesn't seem sure whether the tag is <br> or <BR> or even <Br>.

>>> import re
>>> nobr = re.compile('\W*<br.*?>\W*', re.I)
>>> nobr.sub(' ', "an unfortunate <br /> in the middle")
'an unfortunate in the middle'
>>> nobr.sub(' ', "an unfortunate <BR> in the middle")
'an unfortunate in the middle'



More information about the Python-list mailing list