fork download
  1. # http://stackoverflow.com/q/32940035/5290909
  2. #python 2.7.9
  3. import re
  4.  
  5. def reg_exp():
  6. pattern = 'hello'
  7. infile = "hello hello\nhello hello" #modified to test with a string
  8. match_count = 0
  9. lines = 0
  10.  
  11. for line in infile.split("\n"): #modified to test with a string
  12. match = re.findall(pattern, line)
  13. if match:
  14. match_count += len(match)
  15. lines += 1
  16. return (lines, match_count)
  17.  
  18. if __name__ == "__main__":
  19. lines, match_count = reg_exp()
  20. print 'LINES::', lines
  21. print 'MATCHES::', match_count
Success #stdin #stdout 0.01s 8968KB
stdin
Standard input is empty
stdout
LINES:: 2
MATCHES:: 4