Regex for [a-zA-Z0-9\-] with dashes allowed in between but not at the start or end

后端 未结 4 1778
无人共我
无人共我 2021-02-14 08:02

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 accepte

4条回答
  •  [愿得一人]
    2021-02-14 08:22

    The current regex is simple and fairly readable. Rather than making it long and complicated, have you considered applying the other constraints with normal Python string processing tools?

    import re
    
    def fits_pattern(string):
        if (4 <= len(string) <= 25 and
            "--" not in string and
            not string.startswith("-") and
            not string.endswith("-")):
    
            return re.match(r"[a-zA-Z0-9\-]", string)
        else:
            return None
    

提交回复
热议问题