how to determine an 'open' string?

John La Rooy larooy at xtar.co.nz
Thu May 16 19:06:40 EDT 2002


On Thu, 16 May 2002 23:43:46 +0200
holger krekel <pyth at devel.trillke.net> wrote:

> 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
> 
> 
bugger ;o)
we both lose :/
>>> open_quote('"a"""')
'"'

that should be closed, right? or am i misunderstanding the question?

if should return anything that *isn't* quoted like

q('A"quoted bit"B') --> 'AB'

might need more examples of return values, because "the way python treats quotes"
doesn't define that for you 

John

back to the drawing board...



More information about the Python-list mailing list