Re: Checking if string inside quotes?
On May 9, 8:48 pm, [EMAIL PROTECTED] wrote: > On May 9, 2:31 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] > > > [mailto:[EMAIL PROTECTED] Behalf > > Of [EMAIL PROTECTED] > > Sent: Wednesday, May 09, 2007 5:12 PM > > To: [EMAIL PROTECTED] > > Subject: Re: Checking if string inside quotes? > > > On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> 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. -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking if string inside quotes?
On May 9, 2:31 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] Behalf > Of [EMAIL PROTECTED] > Sent: Wednesday, May 09, 2007 5:12 PM > To: [EMAIL PROTECTED] > Subject: Re: Checking if string inside quotes? > > On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking if string inside quotes?
On May 9, 4:31 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] Behalf > Of [EMAIL PROTECTED] > Sent: Wednesday, May 09, 2007 5:12 PM > To: [EMAIL PROTECTED] > Subject: Re: Checking if string inside quotes? > > On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> 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 "nearby" quotes or in quotes at all? import re a='abc"def"ghijk' b=re.sub( r'([\'"])[^\1]*\1', '', a ) b.replace( 'ghi', 'the string' ) #fb: 'abcthe stringjk' edit() Here, you get the entire file -in b-, strings omitted entirely, so you can't write it back. I've used `tokenize' to parse a file, but you don't get precisely your original back. Untokenize rearrages your spacings. Equivalent semantically, so if you want to compile immedately afterwords, you're alright with that. Short example: from tokenize import * import token from StringIO import StringIO a= StringIO( 'abc "defghi" ghi jk' ) from collections import deque b= deque() for g in generate_tokens( a.readline ): if g[0]== token.NAME and g[1]== 'ghi': b.append( ( token.STRING, '"uchoose"' ) ) else: b.append( g ) untokenize( b ) #fb: 'abc "defghi""uchoose"jk ' edit() acb -- http://mail.python.org/mailman/listinfo/python-list
RE: Checking if string inside quotes?
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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of [EMAIL PROTECTED] Sent: Wednesday, May 09, 2007 5:12 PM To: python-list@python.org Subject: Re: Checking if string inside quotes? On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Checking if string inside quotes?
On May 9, 1:39 pm, "Michael Yanowitz" <[EMAIL PROTECTED]> 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