Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function on captured group in re.sub()

Tags:

python

regex

>>> 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?

like image 622
Frank Epps Avatar asked Jun 16 '13 17:06

Frank Epps


Video Answer


1 Answers

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)
like image 153
mgilson Avatar answered Sep 23 '22 07:09

mgilson