Using ascii numbers in regular expression

Chris Rebert clp2 at rebertia.com
Tue Apr 28 08:25:03 EDT 2009


On Tue, Apr 28, 2009 at 4:58 AM, jorma kala <jjkk73 at gmail.com> wrote:
> Thanks very much for your reply.
> What I mean is that I would like to use the ascii number in a regular
> expression pattern.
> For instance, if I want to substitute the occurrences of character 'a' for
> the character 'b' in a string, instead of doing this:
>
> re.subn('a','b','aaaa')
>
> I'd like to specify the ascii number of a (which is 97)
> I tried converting 97 to hexadecimal (with hex()) and tried this

You should go the more direct route, as my function recommendation implied:

assert chr(97) == "a"
re.subn(chr(97),'b','aaaa')

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list