fork download
  1. import re
  2. s='''new File()
  3. new
  4. File()
  5. there is a new File()
  6. new
  7.  
  8.  
  9.  
  10. File()
  11. there is not a matching pattern here File() new
  12. new File() test new File() occurs twice on this line
  13. '''
  14. pattern = r'new\s+File\s*\(\s*\)'
  15. for m in re.finditer(pattern, s):
  16. linenums = [s[:m.start()].count('\n') + 1]
  17. for _ in range(m.group().count('\n')):
  18. linenums.append(linenums[-1] + 1)
  19. print('{} Found on line {}'.format(re.sub(r'\s+', ' ', m.group()), ", ".join(map(str,linenums))))
Success #stdin #stdout 0.02s 9376KB
stdin
Standard input is empty
stdout
new File() Found on line 1
new File() Found on line 2, 3
new File() Found on line 4
new File() Found on line 5, 6, 7, 8, 9
new File() Found on line 11
new File() Found on line 11