[Tutor] RE module is working ?

Karim karim.liateni at free.fr
Wed Feb 2 20:21:39 CET 2011


Hello,

I am trying to subsitute a '""' pattern in '\"\"' namely escape 2 
consecutives double quotes:

    * *In Python interpreter:*

$ python
Python 2.7.1rc1 (r271rc1:86455, Nov 16 2010, 21:53:40)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> expression = *' "" '*
 >>> re.subn(*r'([^\\])?"', r'\1\\"', expression*)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File "/home/karim/build/python/install/lib/python2.7/re.py", line 
162, in subn
     return _compile(pattern, flags).subn(repl, string, count)
   File "/home/karim/build/python/install/lib/python2.7/re.py", line 
278, in filter
     return sre_parse.expand_template(template, match)
   File "/home/karim/build/python/install/lib/python2.7/sre_parse.py", 
line 787, in expand_template
     raise error, "unmatched group"
sre_constants.error: unmatched group

But if I remove '?' I get the following:

 >>> re.subn(r'([^\\])"', r'\1\\"', expression)
(' \\"" ', 1)

Only one substitution..._But this is not the same REGEX._ And the 
count=2 does nothing. By default all occurrence shoul be substituted.

    * *On linux using my good old sed command, it is working with my '?'
      (0-1 match):*

*$* echo *' "" '* | sed *'s/\([^\\]\)\?"/\1\\"/g*'*
  \"\"

*Indeed what's the matter with RE module!?*

*Any idea will be welcome!

Regards
Karim*
*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110202/36bdb399/attachment.html>


More information about the Tutor mailing list