# 1. Import the module that provides support for regular expression # 2. Define a regular expression # 3. Mark regular expressions as raw strings (r'...') # 4. Create a regular expression object that matches the pattern # using re.compile(r'.....') # 5. Use the regular expression object to search or find # certain patterns in the given string # Write code to find all words starting with 'a' or 'e' # in a given string # For example, a string # "Exam 3 covers everything listed for exams 1 and 2. # Exam 3 is cumulative, including questions intentionally # testing previous exam topics, though it does not include turtle. # There was a slight change in topics in Spring 2017, # and final exams tend to change from one semester # to another more than do other exams, so older exams are not # necessarily the best indicator of future exams" import re text = '''Exam 3 covers everything listed for exams 1 and 2. Exam 3 is cumulative, including questions intentionally testing previous exam topics, though it does not include turtle. There was a slight change in topics in Spring 2017, and final exams tend to change from one semester to another more than do other exams, so older exams are not necessarily the best indicator of future exams''' myobj = re.compile(r'\b[AEae]\w*') # compile raw string in to regex object print(myobj) matched_words = myobj.findall(text) # return a list of strings that match pattern print(matched_words) matched_objects = myobj.finditer(text) # return a collection of matched objs print(matched_objects) for obj in matched_objects: print(obj.group(), obj.start(), obj.end())