'How to write this regex pattern in Swagger?
How to properly define the following regex pattern using Swagger Node.js annotations?
* reviews_ratings_description:
* type: string
* minimum: 10
* maximum: 50
* pattern: "^[ a-zA-Z0-9](?!,.*?[^\na-zA-Z0-9!”$%&~^&#<>.?:"'’=@()*\+,\/;\[\\\]\-^_`{|}~]{1}).*?[a-zA-Z0-9!”$%&~^&#<>.?:"'’=@()*\+,\/;\\\\]\-^_`{|}~]$"
Running the code above produces the following error:
{"error":{},"level":"error","message":"uncaughtException: YAMLException: can not read an implicit mapping pair; a colon is missed at line 42, column 159:\n ... \"'’=@()*\\+,\\/;\\[\\\\\\]\\-^_`{|}~]$'\n
Does the pattern string need to be escaped somehow?
Solution 1:[1]
Your Pattern contains ", that can't work.
You have to escape the inner quotes by using a backslash in front like \".
Solution 2:[2]
you need to write it in the following pattern:
pattern: ^([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_=]+)\.([a-zA-Z0-9_\-\+\/=]+)$
and if I take your example you should remove the " from your regex:
pattern: ^[ a-zA-Z0-9](?!,.*?[^\na-zA-Z0-9!”$%&~^&#<>.?:"'’=@()*\+,\/;\[\\\]\-^_`{|}~]{1}).*?[a-zA-Z0-9!”$%&~^&#<>.?:"'’=@()*\+,\/;\\\\]\-^_`{|}~]$
TIP: use one of the online regex build/test ->https://regex101.com/ (my favorite)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Elad Rubi |
