[Tutor] List of regular expressions

Ewald Ertl ewald.ertl at hartter.com
Wed Jun 22 09:17:36 CEST 2005


Hi Shidan!

on Wed, 22 Jun 2005 00:28:44 -0400  Shidan <shidan at gmail.com> wrote :
---------------------------------------------------------------------------------------------

Shidan > Hi I have a list of regular expression patterns like such:
Shidan > 
Shidan > thelist = ['^594694.*','^689.*','^241.*','^241(0[3-9]|1[0145]|2[0-9]|3[0-9]|41|5[1-37]|6[138]|75|8[014579]).*']
Shidan > 
Shidan > 
Shidan > Now I want to iterate thru each of these like:
Shidan > 
Shidan > for pattern in thelist:
Shidan >     regex=re.compile(pattern)
Shidan >     if regex.match('24110'):
Shidan >         the_pattern = pattern
Shidan >         .
Shidan >         .
Shidan >         sys.exit(0)
Shidan > 
Shidan > but in this case it will pick thelist[2] and not the list[3] as I wanted to,
Shidan > how can I have it pick the pattern that describes it better from the list.

Perhaps you can reorder "thelist" in such a way, that the more extact pattern come's first
thelist = ['^594694.*','^689.*','^241(0[3-9]|1[0145]|2[0-9]|3[0-9]|41|5[1-37]|6[138]|75|8[014579]).*','^241.*']

So you cann exit if '^241(0[3-9]|1[0145]|2[0-9]|3[0-9]|41|5[1-37]|6[138]|75|8[014579]).*' is found, because here 
you want 241 followed by the number-combination which is an exacter description as '241.*'.


------------------- end ----------------------
HTH Ewald 



More information about the Tutor mailing list