How can we get to the end of a quote inside a string
apardon at forel.vub.ac.be
Tue Sep 2 15:54:37 CEST 2008
On 2008-08-31, rajmohan.h at gmail.com <rajmohan.h at gmail.com> wrote:
> Hi all,
> Suppose I have a string which contains quotes inside quotes -
> single and double quotes interchangeably -
> s = "a1' b1 " c1' d1 ' c2" b2 'a2"
> I need to start at b1 and end at b2 - i.e. I have to parse the
> single quote strings from inside s.
> Is there an existing string quote parser which I can use or
> should I write a parser myself?
> If somebody could help me on this I would be much obliged.
You could use a combination of split and join in this case.
#use a single quote as a seperator to split the string is a list of substrings
ls = s.split("'")
#remove what comes before the first and after the last single quote
ls = ls[1:-1]
#reassemble the string between the outermost single quotes.
s = "'".join(ls)
#strip spaces in front and after if you wish
s = s.strip()
More information about the Python-list