Maybe you should consider using re module. http://docs.python.org/2/library/re.html