正则表达式为json架构模式

时间:2017-06-21 12:06:51

标签: json regex jsonschema

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

有效格式为:

"乔":" DAY1""米奇":" DAY2"

"乔":" DAY1"

所以"某人"的任何数量的迭代:""某天"用(逗号)分隔。

格式无效:

"乔":" DAY1""米奇":" DAY2",

或者

"乔":" DAY1&#34 ;;"米奇":" 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*\")"
        }
        }
} 

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:0)

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

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

但它在额外空格上失败,因为它测试:

^("\w+"\s*:\s*"\w+"\s*(?:,\s*|$))+$
相关问题