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

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

    Here's something based on Jerub's "naive approach" (naive being his words, not mine!):

    import string
    ALLOWED = frozenset(string.ascii_letters + string.digits + '_' + '-')
    
    def check(mystring):
        return all(c in ALLOWED for c in mystring)
    

    If ALLOWED was a string then I think c in ALLOWED would involve iterating over each character in the string until it found a match or reached the end. Which, to quote Joel Spolsky, is something of a Shlemiel the Painter algorithm.

    But testing for existence in a set should be more efficient, or at least less dependent on the number of allowed characters. Certainly this approach is a little bit faster on my machine. It's clear and I think it performs plenty well enough for most cases (on my slow machine I can validate tens of thousands of short-ish strings in a fraction of a second). I like it.

    ACTUALLY on my machine a regexp works out several times faster, and is just as simple as this (arguably simpler). So that probably is the best way forward.

提交回复
热议问题