t339.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import re
  2. val = re.findall("From","dlkjdsljkdlkdsjlk")
  3. print val
  4. if len(val) == 0 : print "Correct 1"
  5. else : print "InCorrect 1"
  6. val = re.findall("From","dlkjd From kdsjlk")
  7. print val
  8. if len(val) == 1 : print "Correct 2"
  9. else : print "InCorrect 2"
  10. val = re.findall("From","From dlkjd From kdsjlk")
  11. print val
  12. if len(val) == 2 : print "Correct 3"
  13. else : print "InCorrect 3"
  14. val = re.findall("[0-9]+/[0-9]+","1/2 1/3 3/4 1/8 fred 10/0")
  15. print val
  16. if len(val) == 5 : print "Correct 4"
  17. else : print "InCorrect 4"
  18. # Won't work because JS match does not deal with ()
  19. # print re.findall("From .*@(\\S*)","From csev@umich.edu Sat 09:25:14")
  20. # These return either None or a trivial MatchObject with no methods
  21. val = re.search("From","dlkjdsljkdlkdsjlk")
  22. if val is None: print "Correct 5"
  23. else : print "InCorrect 5",val
  24. val = re.search("From","dlkjd From kdsjlk")
  25. if val is not None: print "Correct 6"
  26. else : print "InCorrect 6",val
  27. val = re.search("From","From dlkjd From kdsjlk")
  28. if val is not None: print "Correct 7"
  29. else : print "InCorrect 7",val
  30. val = re.match("From","dlkjdsljkdlkdsjlk")
  31. if val is None: print "Correct 8"
  32. else : print "InCorrect 8",val
  33. val = re.match("From","dlkjd From kdsjlk")
  34. if val is None: print "Correct 9"
  35. else : print "InCorrect 9",val
  36. val = re.match("From","From dlkjd From kdsjlk")
  37. if val is not None: print "Correct 10"
  38. else : print "InCorrect 10",val