使用正则表达式用双引号+字符串替换字符串

Replace string with double quotes + string using regex

我想用(双引号+字符串)替换一个字符串。需要用到python.

输入 : {responseHeader:{status:0,QTime:94}}

输出 : {"responseHeader":{"status":0,"QTime":94}}

尝试 /[^\d\W]+/g regex 得到 仅字符串 但不知道如何 replace(双引号 + 字符串).

试试这个

>>> import re
>>> inp = '{responseHeader:{status:0,QTime:94}}'
>>> re.sub(r'([a-zA-Z]+)',r'""',inp)
'{"responseHeader":{"status":0,"QTime":94}}'
([a-zA-Z]+)

通过 "" 尝试 this.Replace。查看演示。

https://regex101.com/r/sJ9gM7/18#python

import re
p = re.compile(r'([a-zA-Z]+)', re.MULTILINE)
test_str = "{responseHeader:{status:0,QTime:94}}"
subst = "\"\""

result = re.sub(p, subst, test_str)