Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading