How do I verify that a string only contains letters, numbers, underscores and dashes?

前端 未结 11 918
小鲜肉
小鲜肉 2020-11-29 18:51

I know how to do this if I iterate through all of the characters in the string but I am looking for a more elegant method.

11条回答
  •  [愿得一人]
    2020-11-29 19:14

    You could always use a list comprehension and check the results with all, it would be a little less resource intensive than using a regex: all([c in string.letters + string.digits + ["_", "-"] for c in mystring])

提交回复
热议问题