【发布时间】:2023-03-02 21:28:02
【问题描述】:
我有一个很长的 JSON 模式。使用VSCode,需要将partnerName类型替换为string, null(出现20多次,下面的sn-p才出现1次)。
如何搜索和替换整个 partnerName 条目的多行?
从其他question,我尝试使用正则表达式[\n\s]+,(.*\n)+
"partnerName": {(.*\n)+"type": "null"(.*\n)+}
但还是不匹配。
搜索:
"partnerName": {
"type": "null"
},
替换为:
"partnerName": {
"type": "string, null"
},
片段示例:
{
"type": "object",
"properties": {
"node": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"name": {
"type": "string"
},
"description": {
"type": "string"
},
"type": {
"type": "string"
},
"frequency": {
"type": "string"
},
"maxCount": {
"type": "integer"
},
"points": {
"type": "integer"
},
"startAt": {
"type": "string"
},
"endAt": {
"type": "string"
},
"partnerName": {
"type": "null"
},
"action": {
"type": "null"
}
},
"required": [
"id",
"name",
"description",
"type",
"frequency",
"maxCount",
"points",
"startAt",
"endAt",
"partnerName",
"action"
]
}
},
"required": [
"node"
]
},
【问题讨论】:
-
您确实应该解析 json,然后设置您想要的值,但如果您必须使用正则表达式,请尝试:
(partnerName".*\n\s*"type":\s*)"null"。 -
@Mark 不确定解析是什么意思,但正则表达式有效:) 非常感谢!如果您添加为答案,我会将其标记为正确答案
-
你在用javascript和nodejs吗?
-
我是 ios 开发人员,每天使用 swift。但是对于这个模式,我正在使用 ruby (cucumber) 编写一个 rest api 合同测试。所以,它只是使用文本编辑器进行搜索和替换:)