On Thu, 16 Jun 2011 20:48:46 +0800, TheSaint wrote: > Hello, > Is it possible to compile a regex by supplying a list? > > lst= ['good', 'brilliant'. 'solid'] > > re.compile(r'^'(any_of_lst)) > > without to go into a *for* cicle?
How about this? def compile_alternatives(*args): alternatives = map(lambda s: '(' + s + ')', args) alternatives = '|'.join(alternatives) return re.compile(alternatives) >>> x = compile_alternatives('spam', 'ham', 'cheese') >>> x.search('fried egg and spam').group() 'spam' -- Steven -- http://mail.python.org/mailman/listinfo/python-list