Replacing unquoted words only in Python

后端 未结 6 847
情深已故
情深已故 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:36

    This works for your test case.

    import re
    foo = "Hello 'Hello' Nothing"
    mt = re.search(r"[^']Hello(\s+.*)", foo)
    if mt:
       foo = 'Hi' + match.group(1)
    

提交回复
热议问题