how to determine an 'open' string?

holger krekel pyth at devel.trillke.net
Thu May 16 12:59:17 EDT 2002


Harvey Thomas wrote:
> >     asd"""askdjalsdk      # open 
> >     aksdjasd              # closed
> >     asjdkk"kajsd''''      # open
> >     "'asdasd"             # closed
> >     """dontcountoneven"   # open
> > 
> > Any working ideas?
> > 
> >     holger
> > 
> I think this is OK
> 
> import re
> 
> rex = re.compile('"""|\'\'\'|"|\'')
> 
> def quotecompleted(str):
>     global rex
>     f = rex.findall(str)
>     lf = len(f)
>     if lf < 2:  #the trivial cases
>         return lf
>     else:
>         cmp = f[0]
>         i = 1
>         while i < lf:
>             if cmp == f[i]:
>                 if i + 1 == lf:
>                     return 0
>                 else:
>                     cmp = f[i + 1]
>                     i += 2
>             else:
>                 i += 1
>         return 1

i think that's it. very nice! 

i might try to shorten it a bit, though :-)

thanks,

    holger





More information about the Python-list mailing list