how to determine an 'open' string?

Bernhard Herzog bh at intevation.de
Fri May 17 05:39:33 EDT 2002


holger krekel <pyth at devel.trillke.net> writes:

> def open_quote(text, rex=re.compile('"""|\'\'\'|"|\'')):
>     """ return the open quote at the end of text.
>         if all string-quotes are matched, return the
>         empty string. thanks to Harvey Thomas&John La Roy.
>     """
>     rfunc = lambda x,y: x=='' and y  or  not y.startswith(x) and x   or ''
>     quotes = rex.findall(text)
>     return quotes and reduce(rfunc,quotes) or ''

This doesn't even look at backslashes in the string.

>>> open_quote("'\\'")   # should return "'"
''

Since you're trying to parse python source code: why don't you try the
tokenize module?

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                           http://www.mapit.de/



More information about the Python-list mailing list