import re def is_name(text): '''Return whether text is a valid first and last name with an optional middle initial.''' return re.search(r'^\w+ (\d\.)? \w+$', text) != None def extract_parts(float_str): '''Given a string representing a float, return a 2-tuple of the whole and fractional parts.''' match = re.search(r'^(-?\d+)(\.\d+)?$', float_str) return (match.group(1), match.group(2)) def find_long_words(filename): '''Print all words in filename that are at least 15 characters long.''' input_file = open(filename, 'r') source_string = input_file.read().lower() input_file.close() words = re.findall('[a-zA-Z]{15,}', source_string) for word in words: print(word) def print_comments(filename): '''Print all the non-docstring comments in a Python program.''' input_file = open(filename, 'r') # this is a comment with a comments = [] # list of all the comments for line in input_file: line = line.strip() match = re.search(r'#([^"\']*)$', line) if match != None: # yes, there is a comment on this line comments.append(match.group(1)) input_file.close() for comment in comments: # print out all the comments print(comment)