Checking if string inside quotes?
castironpi at gmail.com
castironpi at gmail.com
Wed May 9 22:15:02 EDT 2007
On May 9, 8:48 pm, half.ital... at gmail.com wrote:
> On May 9, 2:31 pm, "Michael Yanowitz" <m.yanow... at kearfott.com> wrote:
>
>
>
> > Thanks, but it is a little more complicated than that,
> > the string could be deep in quotes.
>
> > The problem is in string substitution.
> > Suppose I have a dictionary with MY_IP : "172.18.51.33"
>
> > I need to replace all instances of MY_IP with "172.18.51.33"
> > in the file.
> > It is easy in cases such as:
> > if (MY_IP == "127.0.0.1"):
>
> > But suppose I encounter:"
> > ("(size==23) and (MY_IP==127.0.0.1)")
>
> > In this case I do not want:
> > ("(size==23) and ("172.18.51.33"==127.0.0.1)")
> > but:
> > ("(size==23) and (172.18.51.33==127.0.0.1)")
> > without the internal quotes.
> > How can I do this?
> > I presumed that I would have to check to see if the string
> > was already in quotes and if so remove the quotes. But not
> > sure how to do that?
> > Or is there an easier way?
>
> > Thanks in advance:
> > Michael Yanowitz
>
> > -----Original Message-----
> > From: python-list-bounces+m.yanowitz=kearfott.... at python.org
>
> > [mailto:python-list-bounces+m.yanowitz=kearfott.... at python.org]On Behalf
> > Of half.ital... at gmail.com
> > Sent: Wednesday, May 09, 2007 5:12 PM
> > To: python-l... at python.org
> > Subject: Re: Checking if string inside quotes?
>
> > On May 9, 1:39 pm, "Michael Yanowitz" <m.yanow... at kearfott.com> wrote:
> > > Hello:
>
> > > If I have a long string (such as a Python file).
> > > I search for a sub-string in that string and find it.
> > > Is there a way to determine if that found sub-string is
> > > inside single-quotes or double-quotes or not inside any quotes?
> > > If so how?
>
> > > Thanks in advance:
> > > Michael Yanowitz
>
> > I think the .find() method returns the index of the found string. You
> > could check one char before and then one char after the length of the
> > string to see. I don't use regular expressions much, but I'm sure
> > that's a more elegant approach.
>
> > This will work. You'll get in index error if you find the string at
> > the very end of the file.
>
> > s = """
> > foo
> > "bar"
> > """
> > findme = "foo"
> > index = s.find(findme)
>
> > if s[index-1] == "'" and s[index+len(findme)] == "'":
> > print "single quoted"
> > elif s[index-1] == "\"" and s[index+len(findme)] == "\"":
> > print "double quoted"
> > else:
> > print "unquoted"
>
> > ~Sean
>
> > --http://mail.python.org/mailman/listinfo/python-list
>
> In that case I suppose you'd have to read the file line by line and if
> you find your string in the line then search for the indexes of any
> matching quotes. If you find matching quotes, see if your word lies
> within any of the quote indexes.
>
> #!/usr/bin/env python
>
> file = open("file", 'r')
> findme= "foo"
> for j, line in enumerate(file):
> found = line.find(findme)
> if found != -1:
> quotecount = line.count("'")
> quoteindexes = []
> start = 0
> for i in xrange(quotecount):
> i = line.find("'", start)
> quoteindexes.append(i)
> start = i+1
>
> f = False
> for i in xrange(len(quoteindexes)/2):
> if findme in
> line[quoteindexes.pop(0):quoteindexes.pop(0)]:
> f = True
> print "Found %s on line %s: Single-Quoted" % (findme, j
> +1)
> if not f:
> print "Found %s on line %s: Not quoted" % (findme, j+1)
>
> It's not pretty but it works.
>
> ~Sean
This approach omits double-quoted strings, escaped single-quotes "'a
\'b' my tag", triple-quoted strings, as well as multi-line strings of
any type.
Depends what constraints you can sacrifice. Maybe character-at-a-
time, or manually untokenize the solution above. For generic input,
use mine.
More information about the Python-list
mailing list