Replace string except inside quotes?

Michael J. Fromberger Michael.J.Fromberger at Clothing.Dartmouth.EDU
Fri Dec 3 20:12:48 CET 2004


In article <3064b51d.0412030739.2c301449 at posting.google.com>,
 beliavsky at aol.com wrote:

> The code 
> 
> for text in open("file.txt","r"):
>     print text.replace("foo","bar")[:-1]
> 
> replaces 'foo' with 'bar' in a file, but how do I avoid changing text
> inside single or double quotes? For making changes to Python code, I
> would also like to avoid changing text in comments, either the '#' or
> '""" ... """' kind.

The first part of what you describe isn't too bad, here's some code that 
seems to do what you want:

import re

def replace_unquoted(text, src, dst, quote = '"'):
    r = re.compile(r'%s([^\\%s]|\\[\\%s])*%s' %
                   (quote, quote, quote, quote))

    out = '' ; last_pos = 0
    for m in r.finditer(text):
        out += text[last_pos:m.start()].replace(src, dst)
        out += m.group()
        last_pos = m.end()

    return out + text[last_pos:].replace(src, dst)

Example usage: 
   print replace_unquoted(file('foo.txt', 'r').read(),
                          "foo", "bar")

It's not the most elegant solution in the world.   This code does NOT 
deal with the problem of commented text.  I think it will handle triple 
quotes, though I haven't tested it on that case.

At any rate, I hope it may help you get started.

Cheers,
-M

-- 
Michael J. Fromberger             | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/  | Dartmouth College, Hanover, NH, USA



More information about the Python-list mailing list