t436.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import re
  2. def helper(match,expected):
  3. if type(expected) == str:
  4. if match:
  5. if match.group(0)==expected: print True
  6. else: print match.group(0),expected
  7. else: print "didn't get a match"
  8. else:
  9. if match: print True == expected
  10. else: print False == expected
  11. print "\nSyntax: ."
  12. helper(re.search(".",""),False)
  13. helper(re.search(".","a"),True)
  14. helper(re.search(".a","a"),False)
  15. helper(re.search("a","a"),True)
  16. helper(re.search("a.","a\n"),False)
  17. helper(re.search(".a","ba"),True)
  18. print "\nSyntax: ^"
  19. helper(re.search("^",""),True)
  20. helper(re.search("a^",""),False)
  21. helper(re.search("^a","ba"),False)
  22. helper(re.search("^a","ab"),True)
  23. helper(re.search("^a","\na"),False)
  24. helper(re.search("a^","a"),False)
  25. print "\nSyntax: $"
  26. helper(re.search("$",""),True)
  27. helper(re.search("$a","a"),False)
  28. helper(re.search("a$","a"),True)
  29. helper(re.search("a$","ab"),False)
  30. helper(re.search("a$","a\nb"),False)
  31. helper(re.search("a$","a\n"),True)
  32. print "\nSyntax: *"
  33. helper(re.search("a*",""),"")
  34. helper(re.search("ab*","a"),"a")
  35. helper(re.search("ab*","ab"),"ab")
  36. helper(re.search("ab*","abbbbb"),"abbbbb")
  37. helper(re.search("ab*","ba"),"a")
  38. helper(re.search("ab*","bbbb"),False)
  39. print "\nSyntax: +"
  40. helper(re.search("a+",""),False)
  41. helper(re.search("ab+","a"),False)
  42. helper(re.search("ab+","ab"),"ab")
  43. helper(re.search("ab+","abbbbb"),"abbbbb")
  44. helper(re.search("ab+","ba"),False)
  45. helper(re.search("ab+","bbbb"),False)
  46. print "\nSyntax: ?"
  47. helper(re.search("a?",""),"")
  48. helper(re.search("ab?","a"),"a")
  49. helper(re.search("ab?","ab"),"ab")
  50. helper(re.search("ab?","abbbbb"),"ab")
  51. helper(re.search("ab?","ba"),"a")
  52. helper(re.search("ab?","bbbb"),False)
  53. print "\nSyntax: *?"
  54. helper(re.search("a*?","a"),"")
  55. helper(re.search("ab*?","abbbb"),"a")
  56. helper(re.search("ab*?","a"),"a")
  57. helper(re.search("ab*?",""),False)
  58. print "\nSyntax: +?"
  59. helper(re.search("a+?","a"),"a")
  60. helper(re.search("ab+?","abbbb"),"ab")
  61. helper(re.search("ab+?","a"),False)
  62. helper(re.search("ab+?",""),False)
  63. print "\nSyntax: ??"
  64. helper(re.search("a??","a"),"")
  65. helper(re.search("ab??","abbbb"),"a")
  66. helper(re.search("ab??","a"),"a")
  67. helper(re.search("ab??",""),False)
  68. print "\nSyntax: {m}"
  69. helper(re.search("a{2}","a"),False)
  70. helper(re.search("a{2}","aa"),"aa")
  71. helper(re.search("a{2}","aaa"),"aa")
  72. print "\nSyntax: {m,n}"
  73. helper(re.search("a{1,2}b","b"),False)
  74. helper(re.search("a{1,2}b","ab"),"ab")
  75. helper(re.search("a{1,2}b","aab"),"aab")
  76. helper(re.search("a{1,2}b","aaab"),"aab")
  77. helper(re.search("a{,2}b","b"),"b")
  78. helper(re.search("a{,2}b","ab"),"ab")
  79. helper(re.search("a{,2}b","aab"),"aab")
  80. helper(re.search("a{,2}b","aaab"),"aab")
  81. helper(re.search("a{2,}b","b"),False)
  82. helper(re.search("a{2,}b","ab"),False)
  83. helper(re.search("a{2,}b","aab"),"aab")
  84. helper(re.search("a{2,}b","aaab"),"aaab")
  85. helper(re.search("a{3,5}","aaaaaaaaaa"),"aaaaa")
  86. helper(re.search("a{,5}","aaaaaaaaaa"),"aaaaa")
  87. helper(re.search("a{3,}","aaaaaaaaaa"),"aaaaaaaaaa")
  88. print "\nSyntax: {m,n}?"
  89. helper(re.search("a{1,2}?b","b"),False)
  90. helper(re.search("a{1,2}?b","ab"),"ab")
  91. helper(re.search("a{1,2}?b","aab"),"aab")
  92. helper(re.search("a{1,2}?b","aaab"),"aab")
  93. helper(re.search("a{,2}?b","b"),"b")
  94. helper(re.search("a{,2}?b","ab"),"ab")
  95. helper(re.search("a{,2}?b","aab"),"aab")
  96. helper(re.search("a{,2}?b","aaab"),"aab")
  97. helper(re.search("a{2,}?b","b"),False)
  98. helper(re.search("a{2,}?b","ab"),False)
  99. helper(re.search("a{2,}?b","aab"),"aab")
  100. helper(re.search("a{2,}?b","aaab"),"aaab")
  101. helper(re.search("a{3,5}?","aaaaaaaaaa"),"aaa")
  102. helper(re.search("a{,5}?","aaaaaaaaaa"),"")
  103. helper(re.search("a{3,}?","aaaaaaaaaa"),"aaa")
  104. print "\nSyntax: []"
  105. helper(re.search("[a,b,c]","abc"),"a")
  106. helper(re.search("[a-z]","bc"),"b")
  107. helper(re.search("[A-Z,0-9]","abcdefg"),False)
  108. helper(re.search("[^A-Z]","ABCDEFGaHIJKL"),"a")
  109. helper(re.search("[a*bc]","*"),"*")
  110. print "\nSyntax: |"
  111. helper(re.search("|",""),"")
  112. helper(re.search("|a",""),"")
  113. helper(re.search("a|b","ba"),"b")
  114. helper(re.search("h|ello","hello"),"h")
  115. print "\nSyntax: (...)"
  116. match = re.search("(b*)","bbbba"); print len(match.groups()) == 1
  117. print "\nSyntax: (?...)"
  118. helper(re.search("(?:b*)","bbbba"),'bbbb')
  119. helper(re.search("a(?=b)","a"),False)
  120. helper(re.search("a(?=b)","ab"),"a")
  121. helper(re.search("a(?!b)","a"),"a")
  122. helper(re.search("a(?!b)","ab"),False)