how to determine an 'open' string?

holger krekel pyth at devel.trillke.net
Thu May 16 16:31:29 EDT 2002


Harvey Thomas wrote:
> import re
> 
> rex = re.compile('"""|\'\'\'|"|\'')
> 
> def quotecompleted(str):
>     global rex
>     f = rex.findall(str)
>     lf = len(f)
>     if lf < 2:  #the trivial cases
> ...<13 lines more>

i have come what seems to be the final version :-)

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. Based on ideas from Harvey Thomas.
    """
    rfunc = lambda x,y: x!=y and (x or y) or ''
    quotes = rex.findall(text)
    return quotes and reduce(rfunc,quotes) or ''

regards and thanks for all suggestions,

    holger





More information about the Python-list mailing list