Replacing unquoted words only in Python

后端 未结 6 914
情深已故
情深已故 2021-01-21 04:03

I am looking for a way to replace a word, however only when its not surounded by quotations.

For example Replacing Hello with Hi

6条回答
  •  甜味超标
    2021-01-21 04:37

    Use the substring function find all the occurances of the word you want to replace, for each word look at one index previous to what the substring function returns and see if its a quote.

    eg. ""Hello 'Hello' Nothing"

    Substring function returns 0 -- so of course there is no quote Substring function returns 6 -- check string[5] -- theres a quote, look for next ocurance

    How can you keep checking using the substring function? something like this:

    startindex=0
    while(!done):
          index=substr(string, startindex)
          if(str[index-1] == "'")
                startindex=index 
                continue
    

    from here you'll figure it out

提交回复
热议问题