如何编辑特定的json属性?VSCode上的正则表达式可以工作吗?
我有一个json文件,其中包含如下对象数组:如何编辑特定的json属性?VSCode上的正则表达式可以工作吗?,json,regex,visual-studio-code,Json,Regex,Visual Studio Code,我有一个json文件,其中包含如下对象数组: [ { "_index": "db", "_type": "service", "_id": "1", "_score": 4.0, "_source": { "contentId": "1", "title": "Sample 1", "tokenizer": "whitespace", "keyword": ["sample1", "service"],
[
{
"_index": "db",
"_type": "service",
"_id": "1",
"_score": 4.0,
"_source": {
"contentId": "1",
"title": "Sample 1",
"tokenizer": "whitespace",
"keyword": ["sample1", "service"],
"desp": "Desc this Service",
"contentType": "service",
"url": null,
"contentCategory": "Services",
"contentSubCategory": null,
"assignmentProfile": null,
"employeeId": null,
"assignmentProfileId": null,
"managedRuleId": null,
"contentAcademy": null,
"imageUrl": null,
"metaData": [
"sample1",
"services"
]
}
},
{
"_index": "db",
"_type": "service",
"_id": "2",
"_score": 7.0,
"_source": {
"contentId": "2",
"title": "Sample 2",
"tokenizer": "whitespace",
"keyword": ["sample2", "service"],
"desp": "Desc this Service",
"contentType": "service",
"url": null,
"contentCategory": "Services",
"contentSubCategory": null,
"assignmentProfile": null,
"employeeId": null,
"assignmentProfileId": null,
"managedRuleId": null,
"contentAcademy": null,
"imageUrl": null,
"metaData": [
"sample2",
"services"
]
}
}
]
[
{
"contentId": "1",
"title": "Sample 1",
"tokenizer": "whitespace",
"keyword": ["sample1", "service"],
"desp": "Desc this Service",
"contentType": "service",
"url": null,
"contentCategory": "Services",
"contentSubCategory": null,
"assignmentProfile": null,
"employeeId": null,
"assignmentProfileId": null,
"managedRuleId": null,
"contentAcademy": null,
"imageUrl": null
},
{
"contentId": "2",
"title": "Sample 2",
"tokenizer": "whitespace",
"keyword": ["sample2", "service"],
"desp": "Desc this Service",
"contentType": "service",
"url": null,
"contentCategory": "Services",
"contentSubCategory": null,
"assignmentProfile": null,
"employeeId": null,
"assignmentProfileId": null,
"managedRuleId": null,
"contentAcademy": null,
"imageUrl": null
}
]
我需要删除其中的某些字段。以和元数据
字段开头的所有字段。它需要这样结束:
[
{
"_index": "db",
"_type": "service",
"_id": "1",
"_score": 4.0,
"_source": {
"contentId": "1",
"title": "Sample 1",
"tokenizer": "whitespace",
"keyword": ["sample1", "service"],
"desp": "Desc this Service",
"contentType": "service",
"url": null,
"contentCategory": "Services",
"contentSubCategory": null,
"assignmentProfile": null,
"employeeId": null,
"assignmentProfileId": null,
"managedRuleId": null,
"contentAcademy": null,
"imageUrl": null,
"metaData": [
"sample1",
"services"
]
}
},
{
"_index": "db",
"_type": "service",
"_id": "2",
"_score": 7.0,
"_source": {
"contentId": "2",
"title": "Sample 2",
"tokenizer": "whitespace",
"keyword": ["sample2", "service"],
"desp": "Desc this Service",
"contentType": "service",
"url": null,
"contentCategory": "Services",
"contentSubCategory": null,
"assignmentProfile": null,
"employeeId": null,
"assignmentProfileId": null,
"managedRuleId": null,
"contentAcademy": null,
"imageUrl": null,
"metaData": [
"sample2",
"services"
]
}
}
]
[
{
"contentId": "1",
"title": "Sample 1",
"tokenizer": "whitespace",
"keyword": ["sample1", "service"],
"desp": "Desc this Service",
"contentType": "service",
"url": null,
"contentCategory": "Services",
"contentSubCategory": null,
"assignmentProfile": null,
"employeeId": null,
"assignmentProfileId": null,
"managedRuleId": null,
"contentAcademy": null,
"imageUrl": null
},
{
"contentId": "2",
"title": "Sample 2",
"tokenizer": "whitespace",
"keyword": ["sample2", "service"],
"desp": "Desc this Service",
"contentType": "service",
"url": null,
"contentCategory": "Services",
"contentSubCategory": null,
"assignmentProfile": null,
"employeeId": null,
"assignmentProfileId": null,
"managedRuleId": null,
"contentAcademy": null,
"imageUrl": null
}
]
我想在VSCode上编写一个正则表达式来执行上述操作。我写了以下内容:
"metaData": \[\r\n (.+) ],
用空字符串替换元数据属性。但这不匹配
数组大小为100+,因此有一个表达式可以与之匹配吗?我有一个解决方案,但我不确定它是否适用于VSCode
我在Sublime文本编辑器
上尝试了这个解决方案,效果很好。因此我认为它在VSCode
上也能起作用
解决第一个问题的步骤(以“”开头的行):
在Sublime文本编辑器中打开Json文件
按ctrl+H
,然后单击*
符号以启用正则表达式(确保*
处于启用状态)
然后在查找
部分中键入此正则表达式,替换
部分为空,然后按替换
(不要在替换
部分中写入任何内容)
解决第一个问题的步骤(整个元数据块
):
在Sublime文本编辑器中打开Json文件
按ctrl+H
,然后单击*
符号以启用正则表达式(确保*
处于启用状态)
然后在查找
部分中键入此正则表达式\“metadata[^}]*\]
,并将替换
部分留空,然后按替换
(不要在替换
部分中写入任何内容)
不要忘记启用*
符号(参见下图)
我有一个解决方案,但我不确定它在VSCode
我在Sublime文本编辑器
上尝试了这个解决方案,效果很好。因此我认为它在VSCode
上也能起作用
解决第一个问题的步骤(以“”开头的行):
在Sublime文本编辑器中打开Json文件
按ctrl+H
,然后单击*
符号以启用正则表达式(确保*
处于启用状态)
然后在查找
部分中键入此正则表达式,替换
部分为空,然后按替换
(不要在替换
部分中写入任何内容)
解决第一个问题的步骤(整个元数据块
):
在Sublime文本编辑器中打开Json文件
按ctrl+H
,然后单击*
符号以启用正则表达式(确保*
处于启用状态)
然后在查找
部分中键入此正则表达式\“metadata[^}]*\]
,并将替换
部分留空,然后按替换
(不要在替换
部分中写入任何内容)
不要忘记启用*
符号(参见下图)
在vscode中尝试以下操作:
(^\s*“.*”?\n)|(,\n^\s*“元数据”:\s*[\s\s]+?\s+\])|(^\s{4,}\}$\n)|(^\s{2}(?![\{}])
什么都没有取代
有关完整解释,请参阅
有4个备选方案串连在一起:
(^\s*“.*”?\n)
获取“_索引”:“db”,
例如,包括尾随换行符
(,\n^\s*“metaData”:\s*\[\s]+?\s+\])
获取“metaData”:[…]
和前面的,
位于前面条目的末尾,以消除每个字段中最后一个条目的尾随,
(^\s{4,}\}$\n)
从“\u source”获取作为右大括号的
:{
不再需要
(^\s{2}(?![\{}])
只是为了修复缩进,因为“\u source:{…}
已删除-获取所有行上的前两个空格,后面没有{
或}
。[根据缩进设置,您可能必须使用删除的2
空格。]
如果您只想格式化文档Shift+Alt+F,则可以删除最后一个选项-它应该删除这些空格。不过我注意到,以这种方式重新格式化将重新格式化您的:
"keyword": ["sample1", "service"],
到
不管你是否在乎
在vscode中尝试以下操作:
(^\s*“.*”?\n)|(,\n^\s*“元数据”:\s*[\s\s]+?\s+\])|(^\s{4,}\}$\n)|(^\s{2}(?![\{}])
什么都没有取代
有关完整解释,请参阅
有4个备选方案串连在一起:
(^\s*“.*”?\n)
获取“_索引”:“db”,
例如,包括尾随换行符
(,\n^\s*“metaData”:\s*\[\s]+?\s+\])
获取“metaData”:[…]
和前面的,
位于前面条目的末尾,以消除每个字段中最后一个条目的尾随,
(^\s{4,}\}$\n)
从“\u source”获取作为右大括号的
:{
不再需要
(^\s{2}(?![\{}])
只是为了修复缩进,因为“\u source:{…}
已删除-获取所有行上的前两个空格,后面没有{
或}
。[根据缩进设置,您可能必须使用删除的2
空格。]
如果您只想格式化文档Shift+Alt+F,则可以删除最后一个选项-它应该删除这些空格。不过我注意到,以这种方式重新格式化将重新格式化您的:
"keyword": ["sample1", "service"],
到
不管你是否在乎
在VSCode中,您不需要\r\n
,只需使用\r
或\n
@WiktorStribiżew仍然不匹配。只能匹配到“metaData”:\[/code>您需要