Jonathan Vanasco
Jonathan Vanasco

Reputation: 15680

python regex: don't allow a specific character to repeat

I have a regex

^[a-z][a-z0-9\-]{6,10}[a-z0-9]$

Which matches the following rules:

it's re-used a lot in a module, always alongside some other rules and regexes

while writing out some unit tests, i noticed that it's always used in conjunction with another specific rule.

i can't wrap my head around integrating that rule into this one. i've tried a few dozen approaches with lookbehinds and lookaheads, but have had no luck on isolating to the specific character AND keeping the length requirement.

Upvotes: 1

Views: 626

Answers (1)

user557597
user557597

Reputation:

No repeating hyphen ^[a-z](?:[a-z0-9]|-(?!-)){6,10}[a-z0-9]$

Explained

 ^ [a-z] 
 (?:
      [a-z0-9]   # alnum
   |             # or
      - (?! - )  # hyphen if not followed by hyphen
 ){6,10}
 [a-z0-9] $

Upvotes: 7

Related Questions