I tried to guess your intention but it was not easy:
(?=[a-z0-9-]{1,63})
this look-ahead seem to intend to require the next up to 63 characters to be lowercase ASCII letters or numbers, but in fact, it will succeed even if there’s only one letter followed by anything. So maybe you meant (?=[a-z0-9-]{1,63}$)
to forbid anything else after the legal up to 63 characters.
You seem to want groups of at least one letter or number between the -
but you made the -
optional not really creating a constraint and allowing way to much possibilities which created the overhead of your expression. You can simply say: ([a-z0-9]++-){0,63}[a-z0-9]+
. The groups within the braces require at least one letter or number and require the minus after that, the expression at the end requires at least one letter or number at the end of the expression but will also match the last group without a following -
at the same time. This last group might also be the only one if no -
is contained in your text at all.
Putting it all together you regex becomes: (?=[a-z0-9-]{1,63}$)([a-z0-9]++-){0,63}[a-z0-9]+
. Note that you don’t need a leading ^
or trailing $
if you use the matches
method; it already implies that the string bounds must match the expression bounds.
I hope I got your intention right…