Re: Raw string substitution problem

2010-01-01 Thread Aahz
In article <7p2juvfu8...@mid.individual.net>, Gregory Ewing wrote: >MRAB wrote: >> >> In simple cases you might be replacing with the same string every time, >> but other cases you might want the replacement to contain substrings >> captured by the regex. > >But you can give it a function that ha

Re: Raw string substitution problem

2009-12-19 Thread Rhodri James
On Fri, 18 Dec 2009 17:58:08 -, Alan G Isaac wrote: On 12/17/2009 7:59 PM, Rhodri James wrote: "re.compile('a\\nc')" passes a sequence of four characters to re.compile: 'a', '\', 'n' and 'c'. re.compile() then does it's own interpretation: 'a' passes through as is, '\' flags an escape w

Re: Raw string substitution problem

2009-12-18 Thread Steven D'Aprano
On Sat, 19 Dec 2009 02:24:00 +, MRAB wrote: > Gregory Ewing wrote: >> MRAB wrote: >> >>> In simple cases you might be replacing with the same string every >>> time, but other cases you might want the replacement to contain >>> substrings captured by the regex. >> >> But you can give it a fun

Re: Raw string substitution problem

2009-12-18 Thread MRAB
Gregory Ewing wrote: MRAB wrote: In simple cases you might be replacing with the same string every time, but other cases you might want the replacement to contain substrings captured by the regex. But you can give it a function that has access to the match object and can produce whatever repl

Re: Raw string substitution problem

2009-12-18 Thread Gregory Ewing
MRAB wrote: In simple cases you might be replacing with the same string every time, but other cases you might want the replacement to contain substrings captured by the regex. But you can give it a function that has access to the match object and can produce whatever replacement string it want

Re: Raw string substitution problem

2009-12-18 Thread Lie Ryan
On 12/19/2009 4:59 AM, Alan G Isaac wrote: On 12/18/2009 12:17 PM, MRAB wrote: In simple cases you might be replacing with the same string every time, but other cases you might want the replacement to contain substrings captured by the regex. Of course that "conversion" is needed in the repla

Re: Raw string substitution problem

2009-12-18 Thread Alan G Isaac
On 12/18/2009 12:17 PM, MRAB wrote: In simple cases you might be replacing with the same string every time, but other cases you might want the replacement to contain substrings captured by the regex. Of course that "conversion" is needed in the replacement. But e.g. Vim substitutions handle th

Re: Raw string substitution problem

2009-12-18 Thread Alan G Isaac
On 12/17/2009 7:59 PM, Rhodri James wrote: "re.compile('a\\nc')" passes a sequence of four characters to re.compile: 'a', '\', 'n' and 'c'. re.compile() then does it's own interpretation: 'a' passes through as is, '\' flags an escape which combined with 'n' produces the newline character (0x0a),

Re: Raw string substitution problem

2009-12-18 Thread MRAB
Gregory Ewing wrote: MRAB wrote: Regular expressions and replacement strings have their own escaping mechanism, which also uses backslashes. This seems like a misfeature to me. It makes sense for a regular expression to give special meanings to backslash sequences, because it's a sublanguage

Re: Raw string substitution problem

2009-12-18 Thread Sion Arrowsmith
Gregory Ewing wrote: >MRAB wrote: >> Regular expressions and replacement strings have their own escaping >> mechanism, which also uses backslashes. >This seems like a misfeature to me. It makes sense for >a regular expression to give special meanings to backslash >sequences, because it's a sublan

Re: Raw string substitution problem

2009-12-17 Thread Gregory Ewing
MRAB wrote: Regular expressions and replacement strings have their own escaping mechanism, which also uses backslashes. This seems like a misfeature to me. It makes sense for a regular expression to give special meanings to backslash sequences, because it's a sublanguage with its own syntax. B

Re: Raw string substitution problem

2009-12-17 Thread Rhodri James
On Thu, 17 Dec 2009 20:18:12 -, Alan G Isaac wrote: So is the bottom line the following? A string replacement is not just "converted" as described in the documentation, essentially it is compiled? That depends entirely on what you mean. But that cannot quite be right. E.g., \b will b

Re: Raw string substitution problem

2009-12-17 Thread MRAB
Alan G Isaac wrote: On 12/17/2009 2:45 PM, MRAB wrote: re.compile('a\\nc') _does_ compile to the same as regex as re.compile('a\nc'). However, regex objects never compare equal to each other, so, strictly speaking, re.compile('a\nc') != re.compile('a\nc'). However, having said that, the re mod

Re: Raw string substitution problem

2009-12-17 Thread Alan G Isaac
On 12/17/2009 2:45 PM, MRAB wrote: re.compile('a\\nc') _does_ compile to the same as regex as re.compile('a\nc'). However, regex objects never compare equal to each other, so, strictly speaking, re.compile('a\nc') != re.compile('a\nc'). However, having said that, the re module contains a cache

Re: Raw string substitution problem

2009-12-17 Thread MRAB
Alan G Isaac wrote: Alan G Isaac wrote: >>> re.sub('abc', r'a\nb\n.c\a','123abcdefg') == re.sub('abc', 'a\\nb\\n.c\\a','123abcdefg') == re.sub('abc', 'a\nb\n.c\a','123abcdefg') True Why are the first two strings being treated as if they are the last one? On 12/17/2009

Re: Raw string substitution problem

2009-12-17 Thread Alan G Isaac
Alan G Isaac wrote: >>> re.sub('abc', r'a\nb\n.c\a','123abcdefg') == re.sub('abc', 'a\\nb\\n.c\\a','123abcdefg') == re.sub('abc', 'a\nb\n.c\a','123abcdefg') True Why are the first two strings being treated as if they are the last one? On 12/17/2009 12:19 PM, D'Arcy J.M.

Re: Raw string substitution problem

2009-12-17 Thread MRAB
Alan G Isaac wrote: On 12/17/2009 11:24 AM, Richard Brodie wrote: A raw string is not a distinct type from an ordinary string in the same way byte strings and Unicode strings are. It is a merely a notation for constants, like writing integers in hexadecimal. (r'\n', u'a', 0x16) ('\\n', u'a',

Re: Raw string substitution problem

2009-12-17 Thread D'Arcy J.M. Cain
On Thu, 17 Dec 2009 11:51:26 -0500 Alan G Isaac wrote: > >>> re.sub('abc', r'a\nb\n.c\a','123abcdefg') == re.sub('abc', > 'a\\nb\\n.c\\a',' 123abcdefg') == re.sub('abc', 'a\nb\n.c\a','123abcdefg') > True Was this a straight cut and paste or did you make a manual change? Is tha

Re: Raw string substitution problem

2009-12-17 Thread Alan G Isaac
On 12/17/2009 11:24 AM, Richard Brodie wrote: A raw string is not a distinct type from an ordinary string in the same way byte strings and Unicode strings are. It is a merely a notation for constants, like writing integers in hexadecimal. (r'\n', u'a', 0x16) ('\\n', u'a', 22) Yes, that was

Re: Raw string substitution problem

2009-12-17 Thread Richard Brodie
"Alan G Isaac" wrote in message news:qemdnrut0jvj1lfwnz2dnuvz_vqdn...@rcn.net... > Naturally enough. So I think the right answer is: > > 1. this is a documentation bug (i.e., the documentation >fails to specify unexpected behavior for raw strings), or > 2. this is a bug (i.e., raw strings

Re: Raw string substitution problem

2009-12-17 Thread Alan G Isaac
En Wed, 16 Dec 2009 11:09:32 -0300, Ed Keith escribió: I am having a problem when substituting a raw string. When I do the following: re.sub('abc', r'a\nb\nc', '123abcdefg') I get """ 123a b cdefg """ what I want is r'123a\nb\ncdefg' On 12/16/2009 9:35 AM, Gabriel Genellina wrote: Fr

Re: Raw string substitution problem

2009-12-16 Thread Ed Keith
--- On Wed, 12/16/09, Peter Otten <__pete...@web.de> wrote: > Another possibility: > > >>> print re.sub('abc', lambda m: r'a\nb\n.c\a', > '123abcdefg') > 123a\nb\n.c\adefg I'm not sure whether that is clever, ugly, or just plain strange! I think I'll stick with: >>> m = re.match('^(.*)abc(.*)

Re: Raw string substitution problem

2009-12-16 Thread Peter Otten
Gabriel Genellina wrote: > En Wed, 16 Dec 2009 14:51:08 -0300, Peter Otten <__pete...@web.de> > escribió: > >> Ed Keith wrote: >> >>> --- On Wed, 12/16/09, Gabriel Genellina wrote: >>> Ed Keith escribió: > I am having a problem when substituting a raw string. When I do

Re: Raw string substitution problem

2009-12-16 Thread Gabriel Genellina
En Wed, 16 Dec 2009 14:51:08 -0300, Peter Otten <__pete...@web.de> escribió: Ed Keith wrote: --- On Wed, 12/16/09, Gabriel Genellina wrote: Ed Keith escribió: > I am having a problem when substituting a raw string. When I do the following: > > re.sub('abc', r'a\nb\nc', '123abcdefg') > >

Re: Raw string substitution problem

2009-12-16 Thread Peter Otten
Ed Keith wrote: > --- On Wed, 12/16/09, Gabriel Genellina wrote: > >> From: Gabriel Genellina >> Subject: Re: Raw string substitution problem >> To: python-list@python.org >> Date: Wednesday, December 16, 2009, 9:35 AM >> En Wed, 16 Dec 2009 11:09:32 -0300,

Re: Raw string substitution problem

2009-12-16 Thread Ed Keith
--- On Wed, 12/16/09, Gabriel Genellina wrote: > From: Gabriel Genellina > Subject: Re: Raw string substitution problem > To: python-list@python.org > Date: Wednesday, December 16, 2009, 9:35 AM > En Wed, 16 Dec 2009 11:09:32 -0300, > Ed Keith > escribió: > > &

Re: Raw string substitution problem

2009-12-16 Thread Chris Hulan
On Dec 16, 9:09 am, Ed Keith wrote: > I am having a problem when substituting a raw string. When I do the following: > > re.sub('abc', r'a\nb\nc', '123abcdefg') > > I get > > """ > 123a > b > cdefg > """ > > what I want is > > r'123a\nb\ncdefg' > > How do I get what I want? > > Thanks, > >     -Ed

Re: Raw string substitution problem

2009-12-16 Thread Gabriel Genellina
En Wed, 16 Dec 2009 11:09:32 -0300, Ed Keith escribió: I am having a problem when substituting a raw string. When I do the following: re.sub('abc', r'a\nb\nc', '123abcdefg') I get """ 123a b cdefg """ what I want is r'123a\nb\ncdefg' From http://docs.python.org/library/re.html#re.sub

Raw string substitution problem

2009-12-16 Thread Ed Keith
I am having a problem when substituting a raw string. When I do the following: re.sub('abc', r'a\nb\nc', '123abcdefg') I get """ 123a b cdefg """ what I want is r'123a\nb\ncdefg' How do I get what I want? Thanks, -EdK Ed Keith e_...@yahoo.com Blog: edkeith.blogspot.com -- h