how to determine an 'open' string?
holger krekel
pyth at devel.trillke.net
Thu May 16 17:43:46 EDT 2002
To John La Rooy wrote:
> > is this short enough for you?
> >
> > import re
> >
> > def quoteopen(s):
> > quot=re.compile("(?P<quot>\"\"\"|'''|\"|').*?(?P=quot)")
> > s=quot.sub("",s)
> > return "'" in s or '"' in s
>
> my other version also returns the 'open quote' but
> yours is shorter. you won :-)
NO! you lost :-/
it doesn't work because the rex tries too hard to match.
paste this to your interpreter...
quoteopen('"""a"a""')
and it will match in ".*?" pairs which yields
the wrong result.
> regexes often offer more than one might think...
especial more subtlety :-)
holger
More information about the Python-list
mailing list