Re: Using ascii numbers in regular expression

2009-04-30 Thread Lie Ryan

MRAB wrote:

You're almost there:

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

or better yet:

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


Wouldn't that becomes a literal \x61 instead of a as it is inside raw 
string?

--
http://mail.python.org/mailman/listinfo/python-list


Re: Using ascii numbers in regular expression

2009-04-30 Thread MRAB

Lie Ryan wrote:

MRAB wrote:

You're almost there:

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

or better yet:

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


Wouldn't that becomes a literal \x61 instead of a as it is inside raw 
string?



Yes. The re module will understand the \x sequence within a regular
expression.

The reason I say that the second solution is better is because you say:

How can I use the ascii number of a character in a regular expression
(module re) instead of the character itself?

Certain characters have a special meaning in regular expressions, eg
'*', so if you tried to search for '*' (or '\x2A') you would get an
exception. The solution would be to search for r'\*' or r'\x2A'.
--
http://mail.python.org/mailman/listinfo/python-list


Using ascii numbers in regular expression

2009-04-28 Thread jorma kala
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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using ascii numbers in regular expression

2009-04-28 Thread jorma kala
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','')

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','')

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 c...@rebertia.com wrote:

  On Tue, Apr 28, 2009 at 4:05 AM, jorma kala jjk...@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

--
http://mail.python.org/mailman/listinfo/python-list


Re: Using ascii numbers in regular expression

2009-04-28 Thread Chris Rebert
On Tue, Apr 28, 2009 at 4:58 AM, jorma kala jjk...@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','')

 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','')

Cheers,
Chris
-- 
http://blog.rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using ascii numbers in regular expression

2009-04-28 Thread Chris Rebert
On Tue, Apr 28, 2009 at 4:05 AM, jorma kala jjk...@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
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using ascii numbers in regular expression

2009-04-28 Thread MRAB

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','')
 
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','')
 
but it doesnt work.

I need this because I'm working on non printable characters.
 

[snip]
You're almost there:

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

or better yet:

re.subn(r'\x61','b','')
--
http://mail.python.org/mailman/listinfo/python-list