Update:
This question was an epic failure, but here's the working solution. It's based on Gumbo's answer (Gumbo's was close to working so I chose it as the accepted answer):
Solution:
r'(?=[a-zA-Z0-9\-]{4,25}$)^[a-zA-Z0-9]+(\-[a-zA-Z0-9]+)*$'
Original Question (albeit, after 3 edits)
I'm using Python and I'm not trying to extract the value, but rather test to make sure it fits the pattern.
allowed values:
spam123-spam-eggs-eggs1
spam123-eggs123
spam
1234
eggs123
Not allowed values:
eggs1-
-spam123
spam--spam
I just can't have a dash at the starting or the end. There is a question on here that works in the opposite direction by getting the string value after the fact, but I simply need to test for the value so that I can disallow it. Also, it can be a maximum of 25 chars long, but a minimum of 4 chars long. Also, no 2 dashes can touch each other.
Here's what I've come up with after some experimentation with lookbehind, etc:
# Nothing here
[a-zA-Z0-9]+
at the end is not necessary;(\-[a-zA-Z0-9]+)*
is already covering that.alnum
, and zero or more repetitions of this pattern). In fact, not only was it not necessary, but it actually worked incorrectly. If the stringi-am-string-number-5
was searched against that re, it would returnNone
, because only enough was there to be eaten by the hyphen pattern of the regex. Thanks for all your help man. I've edited my solution.