2016-12-15 145 views
0

我有一个JavaScript正则表达式来修复损坏的JSON对象(我的后端从JSON字符串中删除所有引号,正则表达式再次添加它们)。翻译Javascript正则表达式到Python

var src = '[{ key: any text with spaces, emptykey: , foo: 0}, { key2: other text with spaces, emptykey2: , foo2: 2},]'; 

console.log(src.replace(/(\w+):(\s*)(.*?)(,|})/g, '"$1":$2"$3"$4')); 
// outputs [{ "key" : "any text with spaces", emptykey: "", "foo": "0"},...] 

我需要翻译这个正则表达式替换为python,但我不知道如何包含具有名称后向引用的部分。这里是我的出发点

import json 
    import re 

    invalid_json = '[{ key: any text with spaces, emptykey: , foo: 0}, { key2: other text with spaces, emptykey2: , foo2: 2}]' 
    result = re.sub('/(\w+):(\s*)(.*?)(,|})/g', what to do here in python?, invalid_json) 
    print result 
+1

@MohammadYusufGhazi我将如何使用JavaScript占位符蟒蛇$ 1吗? – ManuKaracho

+1

用\\ 1替换$ 1 – Zaphod

+0

@ManuKaracho'r'\ 1''或''\\ 1'' – MYGz

回答

3
import json 
import re 

invalid_json = '[{ key: any text with spaces, emptykey: , foo: 0}, { key2: other text with spaces, emptykey2: , foo2: 2}]' 
result = re.sub('(\w+):(\s*)(.*?)(,|})', r'"\1":\2"\3"\4', invalid_json) 
print result 
print json.loads(result) 

输出:

[{ "key": "any text with spaces", "emptykey": "", "foo": "0"}, { "key2": "other text with spaces", "emptykey2": "", "foo2": "2"}] 
[{u'emptykey': u'', u'foo': u'0', u'key': u'any text with spaces'}, {u'key2': u'other text with spaces', u'emptykey2': u'', u'foo2': u'2'}]