fork(1) download
  1. # http://stackoverflow.com/q/33587745/5290909
  2. # Match Python IF STATEMENTS
  3. import re
  4.  
  5. if_statement = re.compile(r'''
  6. #if statement (group 1 captures the indentation)
  7. ^([ \t]*) if\b .* $
  8.  
  9. #code
  10. (?:
  11. #comments with any indentation
  12. (?:
  13. \s*?
  14. \n [ \t]* [#].*
  15. )*
  16.  
  17. #Optional elif/else lines
  18. (?:
  19. \s*?
  20. \n\1 el(?:se|if)\b .* $
  21. )?
  22.  
  23. #following lines with more indentation
  24. \s*?
  25. \n\1 [ \t] .*
  26. )*
  27.  
  28. \n? #last newline char
  29. ''', re.MULTILINE | re.VERBOSE)
  30.  
  31.  
  32. test_str = r'''
  33. weight = float(input("How many pounds does your suitcase weigh? "))
  34. if weight > 50:
  35. print("There is a $25 charge for luggage that heavy.")
  36. print("Thank you for your business.")
  37.  
  38. if totalHours <= 40:
  39. regularHours = totalHours
  40. overtime = 0
  41. else:
  42. overtime = totalHours - 40
  43. regularHours = 40
  44. return hourlyWage*regularHours + (1.5*hourlyWage)*overtime
  45.  
  46. if score >= 90:
  47. letter = 'A'
  48. else: # grade must be B, C, D or F
  49. if score >= 80:
  50. letter = 'B'
  51. else: # grade must be C, D or F
  52. if score >= 70:
  53. letter = 'C'
  54. else: # grade must D or F
  55. if score >= 60:
  56. letter = 'D'
  57. else:
  58. letter = 'F'
  59. return letter
  60.  
  61. def letterGrade(score):
  62. if score >= 90:
  63. letter = 'A'
  64. elif score >= 80:
  65. letter = 'B'
  66. elif score >= 70:
  67. letter = 'C'
  68. elif score >= 60:
  69. letter = 'D'
  70. else:
  71. letter = 'F'
  72. return letter
  73.  
  74. def printAllPositive(numberList):
  75. for num in numberList:
  76. if num > 0:
  77. print(num)
  78.  
  79. printAllPositive([3, -5, 2, -1, 0, 7])
  80.  
  81. for i in range(600):
  82. shape.move(dx, dy)
  83. center = shape.getCenter()
  84. x = center.getX()
  85. y = center.getY()
  86. if x < xLow:
  87. dx = -dx
  88. elif x > xHigh:
  89. dx = -dx
  90. if y < yLow:
  91. dy = -dy
  92. elif y > yHigh:
  93. dy = -dy
  94. time.sleep(delay)
  95. '''
  96.  
  97.  
  98. # Loop matches
  99. m = 0
  100. for match in if_statement.finditer(test_str):
  101. m += 1
  102. print( 'Match #%s\t[%s:%s]:\n%s' %(m, match.start(), match.end(), match.group()))
  103.  
  104. if m == 0:
  105. print("NO MATCH")
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
Match #1	[74:159]:
	    if weight > 50:
	        print("There is a $25 charge for luggage that heavy.")

Match #2	[204:361]:
	    if totalHours <= 40:
	        regularHours = totalHours
	        overtime = 0
	    else:
	        overtime = totalHours - 40
	        regularHours = 40

Match #3	[427:830]:
	    if score >= 90:
	        letter = 'A'
	    else:   # grade must be B, C, D or F
	        if score >= 80:
	            letter = 'B'
	        else:  # grade must be C, D or F
	            if score >= 70:
	                letter = 'C'
	            else:    # grade must D or F
	                if score >= 60:
	                    letter = 'D'
	                else:
	                    letter = 'F'

Match #4	[876:1087]:
	    if score >= 90:
	        letter = 'A'
	    elif score >= 80:
	        letter = 'B'
	    elif score >= 70:
	        letter = 'C'
	    elif score >= 60:
	        letter = 'D'
	    else:
	        letter = 'F'

Match #5	[1171:1216]:
	        if num > 0:
	            print(num)

Match #6	[1404:1495]:
	        if x < xLow:
	            dx = -dx
	        elif x > xHigh:
	            dx = -dx

Match #7	[1495:1598]:
	        if y < yLow:
	            dy = -dy
	        elif y > yHigh:
	            dy = -dy