Pattern-match & Replace - help required

Peter Otten __peter__ at web.de
Wed Dec 19 14:16:18 CET 2012


AT wrote:

> I am new to python and web2py framework. Need urgent help to match a
> pattern in an string and replace the matched text.
> 
> I've this string (basically an sql statement):
> stmnt = 'SELECT  taxpayer.id,
>          taxpayer.enc_name,
>          taxpayer.age,
>          taxpayer.occupation
>          FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'
> 
> The requirement is to replace it with this one:
> r_stmnt = 'SELECT  taxpayer.id,
>            decrypt(taxpayer.enc_name),
>            taxpayer.age,
>            taxpayer.occupation
>            FROM taxpayer WHERE (taxpayer.id IS NOT NULL);'
> 
> Can somebody please help?

> The pattern is '%s.enc_%s', and after matching this pattern want to change
> it to 'decrypt(%s.enc_%s)'

after = re.compile(r"(\w+[.]enc_\w+)").sub(r"decrypt(\1)", before)




More information about the Python-list mailing list