0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

下書き

Posted at

サンプル

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} に対応する内容が見つかりませんでした。")
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?