python regular expression tag
Question: I want to change this string

`<p><b> hello world </b></p>. I am playing <b> python </b>`

to:

`<bold><bold>hello world </bold></bold>, I am playing <bold> python </bold>`

I used:

    
    
    import re 
    
    pattern = re.compile(r'\<p>(.*?)\</p>|\<b>(.*?)\</b>')
    
    print re.sub(pattern, r'<bold>\1</bold>', "<p><b>hello world</b></p>. I am playing <b> python</b>")
    

It does not output what I want, it complains error: _unmatched group_

It works in this case:

    
    
    re.sub(pattern, r'<bold>\1</bold>', "<p>hello world</p>. I am playing <p> python</p>")
    

`<bold> hello world </bold>`. I am playing `<bold> python</bold>`


Answer: If you choose not to use regex, then it simple as this:

    
    
    d = {'<p>':'<bold>','</p>':'</bold>','<b>':'<bold>','</b>':'</bold>'}
    s = '<p><b> hello world </b></p>. I am playing <b> python </b>'
    for k,v in d.items():
        s = s.replace(k,v)
    

