Using ascii numbers in regular expression

MRAB google at mrabarnett.plus.com
Tue Apr 28 15:49:07 CEST 2009


jorma kala 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
>  
> re.subn(''\0x61,'b','aaaa')
>  
> but it doesnt work.
> I need this because I'm working on non printable characters.
>  
[snip]
You're almost there:

     re.subn('\x61','b','aaaa')

or better yet:

     re.subn(r'\x61','b','aaaa')



More information about the Python-list mailing list