Search and Replace using regex in python

前端 未结 2 1454
感动是毒
感动是毒 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"}'
    

提交回复
热议问题