>>> base64_encode = lambda url : url.encode('base64').replace('\n', '')
>>> s = '<A HREF="http://www.google.com" ID="test">blah</A>'
>>> re.sub(r'(?<=href=")([\w:/.]+)(?=")', base64_encode(r'\1'), s, flags=re.I)
<A HREF="XDE=" ID="test">blah</A>
The base64 encoding of the string http://www.google.com
is aHR0cDovL3d3dy5nb29nbGUuY29t
not XDE=
, which is the encoding of \1
.
How do I pass the captured group into the function?