Check if String is a concatenation of elements in a list
Question: Is there an elegant way (preferably pythonic too) to check if a String _s_ is
a concatenation of elements of a subset of set _L_? An element of _L_ may
appear more than once in _s_.

For example:

    
    
    L = set(["a", "ab", "c", "e"])
    

Then "abac" is a valid concatenation of elements of a subset of _L_

"aaaaaaa" is also a valid concatenation.

But "ad" is not since "d" not in L.


Answer: 
    import re
    L = ["no", "force", "in", "the", "verse", "can", "stop", "me"]
    # make this: "(?:no|force|in|the|verse|can|stop|me)*$"
    r = re.compile( "(?:" + "|".join(L) + ")*$")
    r.match("shiny")  # -> None
    r.match("canme")  # -> not None
    

That works for the given set of strings. There is a function in the `re`
library to quote strings (escaping `|` etc) so that you can safely make such
an expression at run time.

    
    
    r = re.compile( "(?:"  + "|".join( re.escape(s) for s in L) + ")*$" )
    

It will match no matter how many times the substrings appear; and strange
results might occur, if some of the strings are prefixes of others and so
forth. It may have nasty runtime. If all of the strings are distinguished
easily from each other by their beginnings, it shouldn't.

