Home > Blockchain >  What part of this regex is not supported by json schema and can it be rewritten to support it?
What part of this regex is not supported by json schema and can it be rewritten to support it?

Time:01-05

Apparently json schema doesn't like this regex: ^(?=.{1,63}$)([-a-z0-9]*[a-z0-9])?$

https://regex101.com/r/qsyUoQ/1

I get an error: pattern must be a valid regex. This error means the regex pattern I'm using is invalid according to json schema.

My regex seems to be valid for most other parsers though. and json schema supports positive and negative look aheads and capture groups: https://json-schema.org/understanding-json-schema/reference/regular_expressions.html

Is there some json schema specific escaping I need to do with my pattern?

I'm at a loss to see what it doesn't like about my regex.

The regex I want will do the following:

  • Allow lower case chars, numbers and "-"
  • Can start with but not end with "-"
  • Max length of string cannot exceed 63 chars

CodePudding user response:

You could simplify the pattern to use the character classes and quantifiers without using the lookahead and the capture group.

You can change the quantifiers, matching 0-62 chars allowing the - and a single char without the - as a single char would also mean that it is at the end.

^[-a-z0-9]{0,62}[a-z0-9]$

Regex demo

  •  Tags:  
  • Related