#!/usr/bin/python # popgrep3 - grep for abbreviations of places that say "pop" # version 3: compile a single pattern import fileinput import re popstates = ["CO","ON","MI","WI","MN"] state_re = re.compile(r"\b(?:"+"|".join(popstates)+r")\b") for line in fileinput.input(): if state_re.search(line): print line