What I currently have...
^((([A-Za-z])+([A-Za-z0-9\-])*([a-zA-Z0-9])+)*\.)+$
Rules:
- The first char must either be a "." or [a-zA-Z] (it can only be "." if the string is of length 1)
- It must end in a "."
- before any "." there can only be [a-zA-Z0-9]
- other than a-zA-Z0-9 and . there can be - (hyphens) that is the only otther character set value
- after any "." there can not be a "-"
examples that should match:
.
a.
a-9.
abc.
abc.a-c.abc.
that should not match:
-.
-a.
a-.
a
abc.-bc
ab-.abc
abc.a-@c
..
currently it does not match a.
which is one of the simplest cases. Do you have any suggestions on how to fix it?
As an alternative solution without lookarounds, you can start the string by matching a-zA-Z.
Then use an optional pattern that matches zero or more repetitions of the character class including the hyphen, and ends with matching without the hyphen to prevent it to be present before the dot in the repetition or at the end of the string.
With case insensitive enabled:
In parts
^
Start of string(?:
Non capture group[a-z]
Match a single char a-z(?:
Non capture group[a-z0-9-]*
Match 0+ times any of a-z0-9-[a-z0-9]
End with a-z-9 so that there can not be a-
before the.
)?
Close group and make it optional(?:
Non capture group\.[a-z0-9-]*
Match a.
and 0+ times any of a-z0-9-[a-z0-9]
End with a-z-9 so that there can not be a-
before the.
)*
Close group and repeat it 0+ times)?
Close group and make it optional to also allow a single dot\.
Match a single dot$
End of stringRegex demo