json 模式模式的正则表达式

Regex for json schema pattern

我想用模式验证 json 模式中的人工输入,但我无法找出它的正则表达式。

有效格式为:

"Joe":"day1","Mitch":"day2"

"Joe":"day1"

因此 "somebody":"someday" 的任意迭代次数用 ,(逗号)分隔。

无效格式为:

"Joe":"day1","Mitch":"day2",

"Joe":"day1";"Mitch":"day2"

示例 Json 模式(模式在这里不起作用):

{
    "title": "Test",
    "type": "object",
    "properties": {
"meeting_date": {
            "type": "string",
            "description": "Give me a name and a day with the following format: \"Joe\":\"day1\" ",
            "default": "",
            "pattern": "(\"\w*\")(?::)(\"\w*\")"
        }
        }
} 

您的模式几乎可以正常工作。您只需删除引号前面的反斜杠即可。

("\w*")(?::)("\w*")

您可以在 https://regex101.com/(或一些类似的网站)上测试您的正则表达式。

尝试这个解决方案https://regex101.com/r/vW8m6K/2/

^("\w+":"\w+",)*"\w+":"\w+"$

但是它在额外的空间上失败了,因为它测试:

^("\w+"\s*:\s*"\w+"\s*(?:,\s*|$))+$