サンプル
import re
def extract_content(file_path, keyword):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
# 正規表現で "keyword" の後ろの {} 中の文字を抽出
pattern = rf"{keyword}\s*{{(.*?)}}"
match = re.search(pattern, content, re.DOTALL)
if match:
return match.group(1).strip().splitlines() # 行ごとに分割してリストとして返す
else:
return None
# 使用例
file_path = "example.txt" # 抽出元のファイルパス
keyword = "hoge" # 検索キーワード
result = extract_content(file_path, keyword)
if result:
print("抽出結果:", result)
else:
print(f"{keyword} に対応する内容が見つかりませんでした。")