import re, os regex = r"(?<=(?<=>).)( \[\[CiteRef::atlasofsurveillance2022\]\]<\/blockquote>\n\n
)" files = [f for f in os.listdir('.') if os.path.isfile(f) and f.endswith('.mw')] for f in files: with open(f, 'r') as fp: contents = fp.read() # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, "", contents, 0, re.MULTILINE) if contents != result: print (f) with open(f, 'w') as fp: fp.write(result)