regular expressions use
Paul McGuire
ptmcg at austin.rr.com
Mon Aug 22 08:10:42 EDT 2005
Perhaps a bit more verbose than your Perl regexp, here is a decoder
using pyparsing.
-- Paul
# download pyparsing at http://pyparsing.sourceforge.net
from pyparsing import Word,Combine
# define grammar for matching encoded characters
hexnums = "0123456789ABCDEFabcdef"
encodedChar = Combine( "%" + Word(hexnums,exact=2) )
# define and attach conversion action
def unencode(s,l,toks):
return chr(int(toks[0][1:],16))
encodedChar.setParseAction( unencode )
# transform test string
data = "%2b/dhg-%3b %7E"
print encodedChar.transformString( data )
"""
Prints "+/dhg-; ~":
"""
More information about the Python-list
mailing list