fork download
  1. import re
  2. s='new File()\nnew\nFile()\nthere is a new File()\nnew\n \n \n \nFile()\nthere is not a matching pattern here File() new\nnew File() test new File() occurs twice on this line'
  3. pattern = r'new\s+File\s*\(\s*\)'
  4. for m in re.finditer(pattern, s):
  5. linenums = [s[:m.start()].count('\n') + 1]
  6. for _ in range(m.group().count('\n')):
  7. linenums.append(linenums[-1] + 1)
  8. print('{} Found on line {}'.format(re.sub(r'\s+', ' ', m.group()), ", ".join(map(str,linenums))))
  9.  
Success #stdin #stdout 0.02s 9492KB
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