import re
regex = re.compile(r"(?:(?=(?&V_Obj)){(?=(?:(?&V_KeyVal)(?&Sep_Obj))*?\s*(\"jobID\"\s*:\s*(?&V_Value)))(?=(?:(?&V_KeyVal)(?&Sep_Obj))*?\s*(\"exec\"\s*:\s*(?&V_Value)))(?:(?&V_KeyVal)(?&Sep_Obj))+})(?(DEFINE)(?<Sep_Ary>\s*(?:,(?!\s*[}\]])|(?=\])))(?<Sep_Obj>\s*(?:,(?!\s*[}\]])|(?=})))(?<Str>(?>\"[^\\\"]*(?:\\[\s\S][^\\\"]*)*\"))(?<Numb>(?>[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?|(?:[eE][+-]?\d+)))(?<V_KeyVal>(?>\s*(?&Str)\s*:\s*(?&V_Value)\s*))(?<V_Value>(?>(?&Numb)|(?>true|false|null)|(?&Str)|(?&V_Obj)|(?&V_Ary)))(?<V_Ary>\[(?>\s*(?&V_Value)(?&Sep_Ary))*\s*\])(?<V_Obj>{(?>(?&V_KeyVal)(?&Sep_Obj))*\s*}))", flags=re.MULTILINE)
test_str = ("{\n"
" \"2597401\": [\n"
" {\n"
" \"jobID\": \"2597401\",\n"
" \"account\": \"TG-CCR120014\",\n"
" \"user\": \"charngda\",\n"
" \"pkgT\": {\n"
" \"pgi/7.2- 5\": {\n"
" \"libA\": [\n"
" \"libpgc.so\"\n"
" ],\n"
" \"flavor\": [\n"
" \"default\"\n"
" ]\n"
" }\n"
" },\n"
" \"startEpoch\": \"1338497979\",\n"
" \"runTime\": \"1022\",\n"
" \"execType\": \"user:binary\",\n"
" \"exec\": \"ft.D.64\",\n"
" \"numNodes\": \"4\",\n"
" \"sha1\": \"5a79879235aa31b6a46e73b43879428e2a175db5\",\n"
" \"execEpoch\": 1336766742,\n"
" \"execModify\": \"Fri May 11 15:05:42 2012\",\n"
" \"startTime\": \"Thu May 31 15:59:39 2012\",\n"
" \"numCores\": \"64\",\n"
" \"sizeT\": {\n"
" \"bss\": \"1881400168\",\n"
" \"text\": \"239574\",\n"
" \"data\": \"22504\"\n"
" }\n"
" },\n"
" {\n"
" \"jobID\": \"2597401\",\n"
" \"account\": \"TG-CCR120014\",\n"
" \"user\": \"charngda\",\n"
" \"pkgT\": {\n"
" \"pgi/7.2-5\": {\n"
" \"libA\": [\n"
" \"libpgc.so\"\n"
" ],\n"
" \"flavor\": [\n"
" \"default\"\n"
" ]\n"
" }\n"
" },\n"
" \"startEpoch\": \"1338497946\",\n"
" \"runTime\": \"33\",\n"
" \"execType\": \"user:binary\",\n"
" \"exec\": \"cg.C.64\",\n"
" \"numNodes\": \"4\",\n"
" \"sha1\": \"caf415e011e28b7e4e5b050fb61cbf71a62a9789\",\n"
" \"execEpoch\": 1336766735,\n"
" \"execModify\": \"Fri May 11 15:05:35 2012\",\n"
" \"startTime\": \"Thu May 31 15:59:06 2012\",\n"
" \"numCores\": \"64\",\n"
" \"sizeT\": {\n"
" \"bss\": \"29630984\",\n"
" \"text\": \"225749\",\n"
" \"data\": \"20360\"\n"
" }\n"
" },\n"
" {\n"
" \"jobID\": \"2597401\",\n"
" \"account\": \"TG-CCR120014\",\n"
" \"user\": \"charngda\",\n"
" \"pkgT\": {\n"
" \"pgi/7.2-5\": {\n"
" \"libA\": [\n"
" \"libpgc.so\"\n"
" ],\n"
" \"flavor\": [\n"
" \"default\"\n"
" ]\n"
" }\n"
" },\n"
" \"startEpoch\": \"1338500447\",\n"
" \"runTime\": \"145\",\n"
" \"execType\": \"user:binary\",\n"
" \"exec\": \"mg.D.64\",\n"
" \"numNodes\": \"4\",\n"
" \"sha1\": \"173de32e1514ad097b1c051ec49c4eb240f2001f\",\n"
" \"execEpoch\": 1336766756,\n"
" \"execModify\": \"Fri May 11 15:05:56 2012\",\n"
" \"startTime\": \"Thu May 31 16:40:47 2012\",\n"
" \"numCores\": \"64\",\n"
" \"sizeT\": {\n"
" \"bss\": \"456954120\",\n"
" \"text\": \"426186\",\n"
" \"data\": \"22184\"\n"
" }\n"
" },\n"
" {\n"
" \"jobID\": \"2597401\",\n"
" \"account\": \"TG-CCR120014\",\n"
" \"user\": \"charngda\",\n"
" \"pkgT\": {\n"
" \"pgi/7.2-5\": {\n"
" \"libA\": [\n"
" \"libpgc.so\"\n"
" ],\n"
" \"flavor\": [\n"
" \"default\"\n"
" ]\n"
" }\n"
" },\n"
" \"startEpoch\": \"1338499002\",\n"
" \"runTime\": \"1444\",\n"
" \"execType\": \"user:binary\",\n"
" \"exec\": \"lu.D.64\",\n"
" \"numNodes\": \"4\",\n"
" \"sha1\": \"c6dc16d25c2f23d2a3321d4feed16ab7e10c2cc1\",\n"
" \"execEpoch\": 1336766748,\n"
" \"execModify\": \"Fri May 11 15:05:48 2012\",\n"
" \"startTime\": \"Thu May 31 16:16:42 2012\",\n"
" \"numCores\": \"64\",\n"
" \"sizeT\": {\n"
" \"bss\": \"199850984\",\n"
" \"text\": \"474218\",\n"
" \"data\": \"27064\"\n"
" }\n"
" }\n"
" ]\n"
"}")
subst = "{ $1, $2 }"
result = regex.sub(subst, test_str)
if result:
print(result)
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html