Communitygithub.com

正規表現 vs LLM 構造化テキスト解析

構造化テキスト解析において正規表現とLLMのどちらを選ぶかの判断フレームワーク — まず正規表現で対応し、低信頼度のエッジケースにのみLLMを追加します。

正規表現 vs LLM 構造化テキスト解析 とは?

このスキルは、構造化テキスト(クイズ、フォーム、請求書、文書など)の解析において、正規表現とLLMを効果的に組み合わせるための実践的な判断フレームワークを提供します。主な洞察は、正規表現で95〜98%のケースを安価かつ決定論的に処理し、残りのエッジケースにのみLLMを呼び出すことで、コストと精度のトレードオフを最適化するという点です。このスキルは、Claude Code、Cursor、CodexなどのAIエージェントや、Pythonベースのテキスト処理パイプラインで利用できます。実装は、正規表現パーサー、信頼度スコアリング、低信頼度アイテムのみを処理するLLMバリデーター、そしてこれらを統合したハイブリッドパイプラインの4つのステップで構成されます。実際のプロダクション環境でのメトリクス(410アイテムのクイズ解析パイプライン)では、正規表現の成功率98%、LLM呼び出しは全体の約2%に抑えられ、全LLM方式と比較して約95%のコスト削減を達成しています。このスキルは、コストが重要な構造化テキスト処理において、正規表現の確実性とLLMの柔軟性をバランスよく活用したい開発者にとって非常に有用です。

対応✓Claude Code~Codex CLI~Cursor
npx skills add https://github.com/affaan-m/everything-claude-code/tree/main/skills/regex-vs-llm-structured-text

お気に入りのAIに質問する

このエージェントスキルを事前に読み込んだ状態で新しいチャットを開きます。

ドキュメント

正規表現 vs LLM 構造化テキスト解析 は何をしますか?

構造化テキスト(クイズ、フォーム、請求書、文書)を解析するための実践的な判断フレームワーク。主要な洞察:正規表現は95〜98%のケースを安価かつ決定論的に処理します。残りのエッジケースにのみLLMの高価な呼び出しを予約します。

アクティベートするタイミング

  • 繰り返しパターンを持つ構造化テキスト(質問、フォーム、テーブル)の解析
  • テキスト抽出における正規表現とLLMの選択
  • 両方のアプローチを組み合わせたハイブリッドパイプラインの構築
  • テキスト処理におけるコスト/精度のトレードオフの最適化

判断フレームワーク

テキスト形式は一貫性があり繰り返しパターンを持つか?
├── はい(90%以上がパターンに従う)→ 正規表現から開始
│   ├── 正規表現で95%以上処理可能 → 完了、LLM不要
│   └── 正規表現で95%未満 → エッジケースのみLLMを追加
└── いいえ(自由形式でばらつきが大きい)→ LLMを直接使用

アーキテクチャパターン

ソーステキスト
    │
    ▼
[正規表現パーサー] ─── 構造を抽出(95〜98%の精度)
    │
    ▼
[テキストクリーナー] ─── ノイズを除去(マーカー、ページ番号、アーティファクト)
    │
    ▼
[信頼度スコアラー] ─── 低信頼度の抽出をフラグ付け
    │
    ├── 高信頼度(≥0.95)→ 直接出力
    │
    └── 低信頼度(<0.95)→ [LLMバリデーター] → 出力

実装

1. 正規表現パーサー(大部分を処理)

import re
from dataclasses import dataclass

@dataclass(frozen=True)
class ParsedItem:
    id: str
    text: str
    choices: tuple[str, ...]
    answer: str
    confidence: float = 1.0

def parse_structured_text(content: str) -> list[ParsedItem]:
    """正規表現パターンを使って構造化テキストを解析する。"""
    pattern = re.compile(
        r"(?P<id>\d+)\.\s*(?P<text>.+?)\n"
        r"(?P<choices>(?:[A-D]\..+?\n)+)"
        r"Answer:\s*(?P<answer>[A-D])",
        re.MULTILINE | re.DOTALL,
    )
    items = []
    for match in pattern.finditer(content):
        choices = tuple(
            c.strip() for c in re.findall(r"[A-D]\.\s*(.+)", match.group("choices"))
        )
        items.append(ParsedItem(
            id=match.group("id"),
            text=match.group("text").strip(),
            choices=choices,
            answer=match.group("answer"),
        ))
    return items

2. 信頼度スコアリング

LLMによるレビューが必要な可能性があるアイテムをフラグ付けします。

@dataclass(frozen=True)
class ConfidenceFlag:
    item_id: str
    score: float
    reasons: tuple[str, ...]

def score_confidence(item: ParsedItem) -> ConfidenceFlag:
    """抽出の信頼度をスコアリングし、問題をフラグ付けする。"""
    reasons = []
    score = 1.0

    if len(item.choices) < 3:
        reasons.append("few_choices")
        score -= 0.3

    if not item.answer:
        reasons.append("missing_answer")
        score -= 0.5

    if len(item.text) < 10:
        reasons.append("short_text")
        score -= 0.2

    return ConfidenceFlag(
        item_id=item.id,
        score=max(0.0, score),
        reasons=tuple(reasons),
    )

def identify_low_confidence(
    items: list[ParsedItem],
    threshold: float = 0.95,
) -> list[ConfidenceFlag]:
    """信頼度しきい値を下回るアイテムを返す。"""
    flags = [score_confidence(item) for item in items]
    return [f for f in flags if f.score < threshold]

3. LLMバリデーター(エッジケースのみ)

def validate_with_llm(
    item: ParsedItem,
    original_text: str,
    client,
) -> ParsedItem:
    """LLMを使用して低信頼度の抽出を修正する。"""
    response = client.messages.create(
        model="claude-haiku-4-5-20251001",  # バリデーションには最も安価なモデル
        max_tokens=500,
        messages=[{
            "role": "user",
            "content": (
                f"このテキストから質問、選択肢、回答を抽出してください。\n\n"
                f"テキスト: {original_text}\n\n"
                f"現在の抽出結果: {item}\n\n"
                f"必要に応じて修正したJSONを返すか、正確であれば'CORRECT'を返してください。"
            ),
        }],
    )
    # LLMのレスポンスを解析し、修正されたアイテムを返す...
    return corrected_item

4. ハイブリッドパイプライン

def process_document(
    content: str,
    *,
    llm_client=None,
    confidence_threshold: float = 0.95,
) -> list[ParsedItem]:
    """完全なパイプライン: 正規表現 → 信頼度チェック → エッジケースにLLM。"""
    # ステップ1: 正規表現による抽出(95〜98%を処理)
    items = parse_structured_text(content)

    # ステップ2: 信頼度スコアリング
    low_confidence = identify_low_confidence(items, confidence_threshold)

    if not low_confidence or llm_client is None:
        return items

    # ステップ3: LLMによるバリデーション(フラグが立ったアイテムのみ)
    low_conf_ids = {f.item_id for f in low_confidence}
    result = []
    for item in items:
        if item.id in low_conf_ids:
            result.append(validate_with_llm(item, content, llm_client))
        else:
            result.append(item)

    return result

実績メトリクス

プロダクションのクイズ解析パイプライン(410アイテム)より。

メトリクス値
正規表現成功率98.0%
低信頼度アイテム8(2.0%)
必要なLLM呼び出し約5回
全LLM方式と比較したコスト削減約95%
テストカバレッジ93%

ベストプラクティス

  • 正規表現から開始する — 不完全な正規表現でも改善のベースラインとなる
  • 信頼度スコアリングを使用する — LLMの支援が必要な箇所をプログラム的に特定する
  • 最も安価なLLMを使用する — バリデーションにはHaikuクラスのモデルで十分
  • 解析済みアイテムをミューテーションしない — クリーニング/バリデーションの各ステップでは新しいインスタンスを返す
  • パーサーにはTDDが有効 — まず既知のパターンのテストを書き、その後エッジケースのテストを追加する
  • メトリクスをログに記録する — 正規表現成功率、LLM呼び出し回数などでパイプラインの健全性を追跡する

避けるべきアンチパターン

  • 正規表現で95%以上処理できる場合にすべてのテキストをLLMに送る(高コストで低速)
  • 自由形式でばらつきの大きいテキストに正規表現を使用する(ここではLLMの方が適切)
  • 信頼度スコアリングをスキップし、正規表現が「うまくいく」と期待する
  • クリーニング/バリデーションのステップで解析済みオブジェクトをミューテーションする
  • エッジケース(不正な入力、フィールド欠落、エンコーディング問題)をテストしない

使用するタイミング

  • クイズ/試験問題の解析
  • フォームデータの抽出
  • 請求書/領収書の処理
  • 文書構造の解析(ヘッダー、セクション、テーブル)
  • コストが重要な、繰り返しパターンを持つ構造化テキスト全般

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

accessibility

Design, implement, and audit inclusive digital products using WCAG 2.2 Level AA. Use when building or auditing UI that must meet WCAG 2.2 Level AA, or when reviewing a change for keyboard, contrast, or screen-reader support.

affaan-m/claude-api

Anthropic Claude API patterns for Python and TypeScript. Covers Messages API, streaming, tool use, vision, extended thinking, batches, prompt caching, and Claude Agent SDK. Use when building applications with the Claude API or Anthropic SDKs.

affaan-m/everything-claude-code

End-to-end marketing campaign planning and execution. Covers audience research, positioning, campaign angle definition, landing page copy, email sequences, social posts, ad copy, short-form video scripts, and content calendars. Use as the orchestration layer for multi-channel product launches. Use when planning or executing a multi-channel product launch, or producing landing page, email, social, or ad copy.

affaan-m/everything-claude-code

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/everything-claude-code-conventions

Development conventions and patterns for everything-claude-code. JavaScript project with conventional commits.

affaan-m/frontend-design

Create distinctive, production-grade frontend interfaces with high design quality. Use when the user asks to build web components, pages, or applications and the visual direction matters as much as the code quality.

affaan-m/gget

gget CLI and Python workflow for quick genomic database queries, sequence lookup, BLAST-style searches, enrichment checks, and reproducible bioinformatics evidence logs.

affaan-m/literature-review

Systematic literature-review workflow for academic, biomedical, technical, and scientific topics, including search planning, source screening, synthesis, citation checks, and evidence logging.

affaan-m/motion-ui

Production-ready UI motion system for React/Next.js. Use when implementing animations, transitions, or motion patterns.

affaan-m/project-guidelines-example

Example project-specific skill template based on a real production application.

affaan-m/pubmed-database

Direct PubMed and NCBI E-utilities search workflows for biomedical literature, MeSH queries, PMID lookup, citation retrieval, and API-backed literature monitoring.

affaan-m/scholar-evaluation

Structured scholarly-work evaluation for papers, proposals, literature reviews, methods sections, evidence quality, citation support, and research-writing feedback.

affaan-m/uspto-database

USPTO patent and trademark data workflow for official record lookup, PatentSearch queries, TSDR checks, assignment data, and reproducible IP research logs.

agent-architecture-audit

Full-stack diagnostic for agent and LLM applications. Audits the 12-layer agent stack for wrapper regression, memory pollution, tool discipline failures, hidden repair loops, and rendering corruption. Produces severity-ranked findings with code-first fixes. Essential for developers building agent applications, autonomous loops, or any LLM-powered feature. Use when an agent or LLM feature misbehaves and the failing layer is unknown, or before shipping an agent stack.

agent-eval

Head-to-head comparison of coding agents (Claude Code, Aider, Codex, etc.) on custom tasks with pass rate, cost, time, and consistency metrics. Use when choosing between coding agents, or when a change to an agent setup needs measured pass rate, cost, and time rather than an impression.

agent-harness-construction

Design and optimize AI agent action spaces, tool definitions, and observation formatting for higher completion rates. Use when defining or revising an agent

agentic-engineering

Operate as an agentic engineer using eval-first execution, decomposition, and cost-aware model routing. Use when planning or executing engineering work that agents will carry out end to end.

agentic-os

Build persistent multi-agent operating systems on Claude Code. Covers kernel architecture, specialist agents, slash commands, file-based memory, scheduled automation, and state management without external databases. Use when building a persistent multi-agent system on Claude Code with its own memory, commands, and scheduling.

agent-introspection-debugging

Structured self-debugging workflow for AI agent failures using capture, diagnosis, contained recovery, and introspection reports. Use when an agent run fails and you need a reproducible diagnosis instead of a retry.

agent-payment-x402

Add x402 payment execution to AI agents with per-task budgets, spending controls, and non-custodial wallets. Supports Base through agentwallet-sdk and X Layer through OKX Payments / OKX Agent Payments Protocol. Use when an agent must pay for something itself and needs per-task budgets, spending controls, and a non-custodial wallet.

関連スキル