Search and Replace using regex in python

前端 未结 2 1455
感动是毒
感动是毒 2021-01-26 10:52
str = \'{\"name\": \"John\", \"company\": \"AB\"C corp\", \"id\": \"12g: \"123 12-12\"}\'

B\"C replace with empty string \'\'

相关标签:
2条回答
  • 2021-01-26 11:32

    You can try this!

    check if you have a character, (a colon or space or both) that is followed by " and by a \w(character or number).

    >>> s
    '{"name": "John", "company": "AB"C corp", "id": "12g: "123 12-12"}'
    >>> re.sub('\w[: ]*"\w','',s)
    '{"name": "John", "company": "A corp", "id": "1223 12-12"}'
    
    0 讨论(0)
  • 2021-01-26 11:51

    Do you mean \W"\W and \w: "\d are the only cases you're worrying about?

    Then try this:

    str = re.sub(r'([a-zA-Z](\: )?"[A-Z0-9])', "", str)
    
    0 讨论(0)
提交回复
热议问题