prefix matching

Jeff Epler jepler at unpythonic.net
Wed May 26 15:30:59 EDT 2004


Here's another simple approach:
    def startswith_one(s, prefixes):
        for p in prefixes:
            if s.startswith(p): return True
        return False
If speed is important and "prefixes" doesn't change frequently, then
coding a FSM in C is the way to go.  The last time this question went
around, I learned that REs like
    a|b|c
essentially tries the alternatives one after another, rather than
compiling into an FSM like I learned in school, so the amount of time
taken is proportional to the length of the total RE, not the longest
alternative.

Jeff




More information about the Python-list mailing list