0

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
5
  • You don't really need the lookahead and the capture group, and it does not have to be optional as well. Try ^[-a-z0-9]{0,62}[a-z0-9]$ regex101.com/r/LCsubB/1 Commented Jan 4, 2022 at 20:20
  • Please make that an answer and I will mark as correct. Tested and confirmed it works for me and json schema doesn't complain about it. Commented Jan 4, 2022 at 20:23
  • Glad it works for you. I posted an answer, but why your pattern does not work I am not sure. Perhaps another answer will be posted that can explain the reason. Commented Jan 4, 2022 at 20:26
  • Please also note that RegEx support is very much an implementation-level thing since not all platforms fully support ECMA 262. Commented Jan 4, 2022 at 21:40
  • 1
    "the regex pattern I'm using is invalid according to json schema" - what implementation are you using? have you checked its documentation to see if there are any limitations in its regex support? have you filed a bug report? Commented Jan 4, 2022 at 23:46

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.