Using dictionary to hold regex patterns?

Gilles Ganault nospam at nospam.com
Sun Nov 23 12:40:05 EST 2008


Hello

After downloading a web page, I need to search for several patterns,
and if found, extract information and put them into a database.

To avoid a bunch of "if m", I figured maybe I could use a dictionary
to hold the patterns, and loop through it:

======
pattern = {}
pattern["pattern1"] = ">.+?</td>.+?>(.+?)</td>"
for key,value in pattern.items():
	response = ">whatever</td>.+?>Blababla</td>"

	#AttributeError: 'str' object has no attribute 'search'
	m = key.search(response)
	if m:
		print key + "#" + value
======

Is there a way to use a dictionary this way, or am I stuck with
copy/pasting blocks of "if m:"?

Thank you.



More information about the Python-list mailing list