regular expressions use

max(01)* max2 at fisso.casa
Mon Aug 22 06:08:02 EDT 2005


hi everyone.

i would like to do some uri-decoding, which means to translate patterns 
like "%2b/dhg-%3b %7E" into "+/dhg-; ~": in practice, if a sequence like 
"%2b" is found, it should be translated into one character whose hex 
ascii code is 2b.

i did this:

...
import re
import sys

modello = re.compile("%([0-9a-f][0-9a-f])", re.IGNORECASE)

def funzione(corrispondenza):
   return chr(eval('0x' + corrispondenza.group(1)))

for riga in sys.stdin:
   riga = modello.sub(funzione, riga)
   sys.stdout.write(riga)
...

please comment it. can it be made easily or more compactly? i am a 
python regexp novice.

bye

max

ps: i was trying to pythonate this kind of perl code:

$riga =~ s/%([A-Fa-f0-9][A-Fa-f0-9])/chr(hex($1))/ge;



More information about the Python-list mailing list