#!/usr/local/bin/python import re # this is an example showing a coroutine matching with regex def regexmatcher (pattern): regex=re.compile(pattern) while True: line = (yield) # Coroutine: Wait here for input matches = regex.findall(line) # Does matching here yield matches # Returns match, pauses here # Construct and compile the pattern datepattern=r"[0-9]{1,2}[/\.-][0-9]{1,2}[/\.-](?:19[0-9]{2}|200[0-9]|2010|2011)" matcher = regexmatcher(datepattern) matcher.next() # Get to the first (yield) matches=matcher.send("10/10/2009") # Send the string, get list of matches if (not matches == None): # No matches = None for match in matches: # If matches, iterate through list print match matcher.next() matches=matcher.send("10/10/1984 9/1/2009") for match in matches: print match matcher.next() matches=matcher.send("12/9/2011 Greg Kesden1/9/2010") for match in matches: print match # Just for fun...no matches here matcher.next() matches=matcher.send("Greg Philip Carrier Alex Richard") for match in matches: print match