I'm trying to recursively replace a string with another string in Python. I'm aware of this thread, but coming from other languages I'm amazed — is it really that hard? Is there no way of using a one liner to do this?
astring="<li><a href="#Quick Start">Quick Start*</li></li>
<li><a href="#Parsing a Document">Parsing a Document*</li></li>
<ul>
<li><a href="#Parsing HTML">Parsing HTML*</li></li>
<li><a href="#Parsing XML">Parsing XML*</li></li>"
tweaked = re.sub(r"\*",r"</a>", astring)
I thought the r
meant recursive but it doesnt seem to do it here. Is it really this hard for a simple replace?
I've tried string.replace
- which didn't work, I think its the newlines maybe? Then I tried string.translate
which wanted the same number of characters in the replace string which didn't work for this example and was too many lines of code. I've tried numerous versions of this, what am I doing wrong?
Maybe I misunderstand recursive? I thought it meant 'not one match, keep going till the end' sort of thing? I want to replace the * with the </a>
. the astring
part is just an example and not the actual string I'm trying to replace as it's huge. (please also excuse my newbness)
PLEASE VOTE THIS QUESTION DOWN TO OBLIVION