Trying to get to grips with regular expressions in Python, I'm trying to output some HTML highlighted in part of a URL. My input is
images/:id/size
my output should be
images/<span>:id</span>/size
If I do this in Javascript
method = 'images/:id/size';
method = method.replace(/\:([a-z]+)/, '<span>$1</span>')
alert(method)
I get the desired result, but if I do this in Python
>>> method = 'images/:id/huge'
>>> re.sub('\:([a-z]+)', '<span>$1</span>', method)
'images/<span>$1</span>/huge'
I don't, how do I get Python to return the correct result rather than $1
? Is re.sub
even the right function to do this?