# # Pattern matching demo program # # Matt Bishop, MHI 289I, Winter 2018 # import re import sys # # loop until end of file # while True: # # read in the pattern # try: pat = raw_input("Pattern: ") except EOFError: break except Exception as msg: print msg continue # # now loop, matching strings (or not) # while True: # # read in a string # try: print "The pattern is:", pat text = raw_input("Text to match: ") except EOFError: sys.exit(0) except Exception as msg: print msg continue # # now do the matching # print "Does it match the begining of the string? ", if re.match(pat, text): print "Yes" else: print "No" print "Does it match anywhere in the string? ", if re.search(pat, text): print "Yes" else: print "No" print "Here are all the matches: ", print re.findall(pat, text)