【发布时间】:2017-05-07 03:43:32
【问题描述】:
我有一些损坏的 JSON 文件想要修复。 问题是 AcquisitionDateTime 字段之一格式不正确:
{
"AcquisitionDateTime": 2016-04-28T17:09:39.515625,
}
我想要做的是将值包含在括号中。我可以使用正则表达式轻松做到这一点:
perl -pi -e 's/\"AcqDateTime\": (.*),/\"AcqDateTime\": \"\1\",/g' t.json
现在,我想扩展正则表达式,以便在 JSON 未损坏的情况下,内容不会被两次包裹在“”中。我面临的问题是我不知道如何混合前瞻、if/then 语句和捕获组。这是我的尝试:
Lookahead, if you find a ", then capture what is between it. Else capture everything.
perl -pi -e 's/\"AcqDateTime\": (?(?=\")\"(.*)\"|(.*)),/\"AcqDateTime:\" \"\1\",/g' t.json
这是我有兴趣更正的部分:
Lookahead for a \" -> if yes, then capture without it. \"(.*)\" Else capture all (.*)
(?(?=\")\"(.*)\"|(.*)),
有人可以向我解释我做错了什么吗?
提前致谢。
【问题讨论】:
-
提示:
\1应该是$1。并且无需逃避所有那些"。