Python text validation: a-z and comma (",")
Question: I need to check that some text only contains lower-case letters a-z and a
comma (",").

What is the best way to do this in Python?


Answer: 
    import re
    def matches(s):
        return re.match("^[a-z,]*$", s) is not None
    

Which gives you:

    
    
    >>> matches("tea and cakes")
    False
    >>> matches("twiddledee,twiddledum")
    True
    

You can optimise a bit with re.compile:

    
    
    import re
    matcher = re.compile("^[a-z,]*$")
    def matches(s):
        return matcher.match(s) is not None
    

