Conversion of perl based regex to python method

Andrew Robert andrew.arobert at gmail.com
Wed May 24 12:22:43 EDT 2006


I have two Perl expressions


If windows:

perl -ple "s/([^\w\s])/sprintf(q#%%%2X#, ord $1)/ge"  somefile.txt

If posix

perl -ple 's/([^\w\s])/sprintf("%%%2X", ord $1)/ge'  somefile.txt



The [^\w\s]  is a negated expression stating that any character
a-zA-Z0-9_, space or tab is ignored.

The () captures whatever matches and throws it into the $1 for
processing by the sprintf

In this case, %%%2X which is a three character hex value.

How would you convert this to a python equivalent using the re or
similar module?

I've begun reading about using re expressions at
http://www.amk.ca/python/howto/regex/ but I am still hazy on implementation.

Any help you can provide would be greatly appreciated.

Thanks,
Andy



More information about the Python-list mailing list