>>> 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?
You pass a function to re.sub
and then you pull the group from there:
def base64_encode(match):
"""
This function takes a re 'match object' and performs
The appropriate substitutions
"""
group = match.group(1)
... #Code to encode as base 64
return result
re.sub(...,base64_encode,s,flags=re.I)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With