How to check if any item from a list of strings is in a big string?

Nobody nobody at nowhere.com
Fri Jul 10 04:53:57 CEST 2009


On Thu, 09 Jul 2009 18:36:05 -0700, inkhorn wrote:

> For one of my projects, I came across the need to check if one of many
> items from a list of strings could be found in a long string.

If you need to match many strings or very long strings against the same
list of items, the following should (theoretically) be optimal:

	r = re.compile('|'.join(map(re.escape,list_items)))
	...
	result = r.search(string)




More information about the Python-list mailing list