繁体   English   中英

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

[英]Replace string with double quotes + string using regex

我想用(双引号+字符串)替换字符串 需要在python中使用它。

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

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

尝试使用/[^\\d\\W]+/g 正则表达式 获取字符串,但不知道如何用(双引号+ string) 替换它。

尝试这个

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

试试看。用"\\1"代替。请参见演示。

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 = "\"\1\""

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM