【问题标题】:How to write this regex pattern in Swagger?如何在 Swagger 中编写这个正则表达式模式?
【发布时间】:2019-07-08 10:23:55
【问题描述】:

如何使用 Swagger Node.js 注释正确定义以下正则表达式pattern?

 *       reviews_ratings_description:
 *         type: string
 *         minimum: 10
 *         maximum: 50
 *         pattern: "^[ a-zA-Z0-9](?!,.*?[^\na-zA-Z0-9!”$%&~^&#<>.?:"'’=@()*\+,\/;\[\\\]\-^_`{|}~]{1}).*?[a-zA-Z0-9!”$%&~^&#<>.?:"'’=@()*\+,\/;\\\\]\-^_`{|}~]$"    

运行上面的代码会产生以下错误:

{"error":{},"level":"error","message":"uncaughtException: YAMLException: can not read an implicit mapping pair; a colon is missed at line 42, column 159:\n     ... \"'’=@()*\\+,\\/;\\[\\\\\\]\\-^_`{|}~]$'\n

pattern 字符串是否需要以某种方式转义?

【问题讨论】:

    标签: node.js yaml swagger


    【解决方案1】:

    您的 Pattern 包含“,无法正常工作。

    您必须通过在前面使用反斜杠来转义内引号,例如 \"。

    【讨论】:

    • 首先我使用了单引号。
    【解决方案2】:

    您需要按照以下模式编写它:

     pattern: ^([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-\+\/=]+)$
    

    如果我以你的为例,你应该从你的正则表达式中删除":

     pattern: ^[ a-zA-Z0-9](?!,.*?[^\na-zA-Z0-9!”$%&~^&#<>.?:"'’=@()*\+,\/;\[\\\]\-^_`{|}~]{1}).*?[a-zA-Z0-9!”$%&~^&#<>.?:"'’=@()*\+,\/;\\\\]\-^_`{|}~]$ 
    

    提示: 使用在线正则表达式构建/测试之一 ->https://regex101.com/(我最喜欢的)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多