将Javascript正则表达式翻译为Python

时间:2016-12-15 10:37:04

标签: python regex

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

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 个答案:

答案 0 :(得分: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'}]