我有以下包含json对象的txt文件。
dtype
到目前为止,我已经定义了以下提取json对象的表达式。
[2018-12-10 06:30:38]..再同步健康数据...
[2018-12-10 06:30:44]JSON回调处理--->>> 同步运动数据 [2018-12-10 06:30:44]同步运动健康数据json:{ "day" : 10, "items" : [ { "activeTime" : 5, "calory" : 0, "distance" : 0, "stepCount" : 0 },
{ "activeTime" : 0, "calory" : 0, "distance" : 0, "stepCount" : 0 }, { "activeTime" : 0, "calory" : 0, "distance" : 0, "stepCount" : 0 } ], "month" : 12, "startTime" : 0,
"timeSpace" : 15, "totalActiveTime" : 81, "totalCalory" : 0, "totalDistance" : 0, "StepCount" : 0, "year" : 2018 }
[2018-12-10 06:30:44]同步运动健康数据items.sise()=96
[2018-12-10 06:30:38]..再同步健康数据...
[2018-12-10 06:30:44]JSON回调处理--->>> 同步运动数据 [2018-12-10 06:30:44]同步运动健康数据json:{ "day" : 10, "items" : [ { "activeTime" : 5, "calory" : 0, "distance" : 0, "stepCount" : 0 },
{ "activeTime" : 0, "calory" : 0, "distance" : 0, "stepCount" : 0 }, { "activeTime" : 0, "calory" : 0, "distance" : 0, "stepCount" : 0 } ], "month" : 12, "startTime" : 0, "timeSpace" : 15, "totalActiveTime" : 81, "totalCalory" : 0, "totalDistance" : 0, "totalCount" : 0, "year" : 2018 }
[2018-12-10 06:30:44]同步运动健康数据items.sise()=96
我只想提取一个包含名为`totalCount的元素的json对象。我应该在我的实际表情中添加些什么?
发布的txt文件,其中替换了json对象的位置
json:.+?(?=\[2)
答案 0 :(得分:1)
您可以从以下位置更改正则表达式,
json:.+?(?=\[2)
到
(?s)json:(?!.*json:.*totalCount.*).*?totalCount.*?(?=\[2)
实现只匹配其中包含totalCount
的json。
此否定的前瞻(?!.*json:)
可确保在看到字符串中的另一个json:
时立即放弃先前的json:
匹配项,从而开始与最近的{{ 1}},而不是第一个。