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

前端 未结 11 947
小鲜肉
小鲜肉 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:03

    If it were not for the dashes and underscores, the easiest solution would be

    my_little_string.isalnum()
    

    (Section 3.6.1 of the Python Library Reference)

提交回复
热议问题