import re case_pattern = re.compile(r"case\s(?P[^:]*):\s*.*?new GUIContent\((?P\"[^\"]*\")\);") name_pattern = re.compile(r"\{(?P[^,]*),\s(?P\d*)\}") with open("input.txt", "r", encoding="utf-8") as f: with open("result.txt", "w", encoding="utf-8") as fo: helper = {} t = f.read() for m in name_pattern.finditer(t): helper[m.group("tmp")] = m.group("key") for m in case_pattern.finditer(t): k = helper[m.group("name")] fo.write(f"[{k}] = {m.group('val')},\n")