regex = r"(.w+.(.*?)})" # add the class name after . toget related classes classes = re.findall(regex, infile.read()) for entry in classes: outfile.write(entry[0] + ' ')