On Wed, Jan 12, 2005, Tzu-Ming Chern wrote: > >Hi Python Tutors, > >Is there a more elegant solution to matching multiple regular expressions? > >An example would be: > >lista = ['hello','goodbye','bubblegum'] # lista would contain my regular >expressions I want to match > >My file would look something like this: > >line1: blah blah aldkfslfjlajf hello >line2: blah blah dkajfldjfjkdsfalj zippo >line3: blah blah lkdsjalkfjkj bubblegum >line4: blah blah skdjflsdjlkj ditto > >What I want to do is to only print out those lines that don't contain >those patterns included in lista. So that would be lines 2 and 4. > >Any suggestions?
If the list of regular expressions is at all large, or you want a general solution, something like this might be appropriate: import re relist = ( re.compile(r'pattern1'), re.compile(r'pattern2'), ... ) fh = open('somefile') for line in fh.xreadlines(): for regex in relist: if regex.search(line): break else: print line Bill -- INTERNET: [EMAIL PROTECTED] Bill Campbell; Celestial Software LLC UUCP: camco!bill PO Box 820; 6641 E. Mercer Way FAX: (206) 232-9186 Mercer Island, WA 98040-0820; (206) 236-1676 http://www.celestial.com/ "I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forego their use." -- Galileo Galilei _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor