[Tutor] Question on regular expressions

Andrew Robert andrew.arobert at gmail.com
Wed May 24 18:30:02 CEST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi Everyone,


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.


- --
Thank you,
Andrew Robert
Systems Architect
Information Technologies
MFS Investment Management
Phone:   617-954-5882

E-mail:  arobert at mfs.com
Linux User Number: #201204
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32)

iD8DBQFEdIoKDvn/4H0LjDwRAi24AKDFmRohKFfp13z/M9c8O1LQElGzMgCglcRw
3ERK7FxWejsuFcnDSNdOYjM=
=28Lx
-----END PGP SIGNATURE-----


More information about the Tutor mailing list