Using ascii numbers in regular expression

jorma kala jjkk73 at gmail.com
Tue Apr 28 13:58:08 CEST 2009


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.

Thanks a lot



On Tue, Apr 28, 2009 at 12:45 PM, Chris Rebert <clp2 at rebertia.com> wrote:

>  On Tue, Apr 28, 2009 at 4:05 AM, jorma kala <jjkk73 at gmail.com> wrote:
> > Hi,
> >
> > How can I use the ascii number of a character in a regular expression
> > (module re) instead of the character itself?
> > Thanks very much
>
> I refer you to the chr() and ord() built-in functions, which can
> certainly be used to solve your problem, though they are not
> regex-specific in application.
> http://docs.python.org/library/functions.html
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090428/dee77e3e/attachment.html>


More information about the Python-list mailing list