मुख्य सामग्री पर जाएँ
Skillpedia
एजेंट280k · #241

regex-vs-llm-structured-text

यह क्या करती है

यह system तय करने में मदद करता है कि ऐसे texts से जानकारी निकालने का सबसे अच्छा तरीका कौन सा है जो किसी pattern का पालन करते हैं, जैसे quiz या receipts। यह एक मिला-जुला तरीका इस्तेमाल करता है: पहले यह तेज़ और सस्ते तरीके (regex) से ज़्यादातर जानकारी निकालता है, और फिर सिर्फ़ उन कुछ मामलों को ठीक करने के लिए एक ज़्यादा उन्नत artificial intelligence (LLM) का इस्तेमाल करता है, जिन्हें पहला तरीका नहीं संभाल पाया।

कब इस्तेमाल करें

  • ✓जब आपके पास एक ही format वाले बहुत सारे documents हों जिनसे data निकालना है।
  • ✓यह तय करते समय कि texts पढ़ने के लिए simple तरीका (regex) इस्तेमाल करें या ज़्यादा जटिल तरीका (LLM)।
  • ✓documents से जानकारी निकालते समय पैसे और समय बचाने के लिए।

Skill का लिंक

https://github.com/affaan-m/ECC/blob/main/pi/core/skills/regex-vs-llm-structured-text/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/affaan-m/ECC/blob/main/pi/core/skills/regex-vs-llm-structured-text/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“मैं ऐसी test file से questions और answers कैसे निकालूँ जिसका format बार-बार दोहराया गया है?”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

पहले यह एक search pattern (regex) का इस्तेमाल करके ज़्यादातर जानकारी ढूँढकर निकालता है। फिर जाँचता है कि निकाली गई जानकारी भरोसेमंद है या नहीं। अगर नहीं, तो सिर्फ़ उन समस्या वाले मामलों को ठीक करने के लिए artificial intelligence (LLM) का इस्तेमाल करता है।

तकनीकी सामग्री (मूल SKILL.md)

लेखक का मूल टेक्स्ट, अंग्रेज़ी में, सीधे GitHub से।

Regex vs LLM for Structured Text Parsing

A practical decision framework for parsing structured text (quizzes, forms, invoices, documents). The key insight: regex handles 95-98% of cases cheaply and deterministically. Reserve expensive LLM calls for the remaining edge cases.

When to Activate

  • Parsing structured text with repeating patterns (questions, forms, tables)
  • Deciding between regex and LLM for text extraction
  • Building hybrid pipelines that combine both approaches
  • Optimizing cost/accuracy tradeoffs in text processing

Decision Framework

Is the text format consistent and repeating?
├── Yes (>90% follows a pattern) → Start with Regex
│   ├── Regex handles 95%+ → Done, no LLM needed
│   └── Regex handles <95% → Add LLM for edge cases only
└── No (free-form, highly variable) → Use LLM directly

Architecture Pattern

Source Text
    │
    ▼
[Regex Parser] ─── Extracts structure (95-98% accuracy)
    │
    ▼
[Text Cleaner] ─── Removes noise (markers, page numbers, artifacts)
    │
    ▼
[Confidence Scorer] ─── Flags low-confidence extractions
    │
    ├── High confidence (≥0.95) → Direct output
    │
    └── Low confidence (<0.95) → [LLM Validator] → Output

Implementation

1. Regex Parser (Handles the Majority)

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]:
    """Parse structured text using regex patterns."""
    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. Confidence Scoring

Flag items that may need LLM review:

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

def score_confidence(item: ParsedItem) -> ConfidenceFlag:
    """Score extraction confidence and flag issues."""
    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]:
    """Return items below confidence threshold."""
    flags = [score_confidence(item) for item in items]
    return [f for f in flags if f.score < threshold]

3. LLM Validator (Edge Cases Only)

def validate_with_llm(
    item: ParsedItem,
    original_text: str,
    client,
) -> ParsedItem:
    """Use LLM to fix low-confidence extractions."""
    response = client.messages.create(
        model="claude-haiku-4-5-20251001",  # Cheapest model for validation
        max_tokens=500,
        messages=[{
            "role": "user",
            "content": (
                f"Extract the question, choices, and answer from this text.\n\n"
                f"Text: {original_text}\n\n"
                f"Current extraction: {item}\n\n"
                f"Return corrected JSON if needed, or 'CORRECT' if accurate."
            ),
        }],
    )
    # Parse LLM response and return corrected item...
    return corrected_item

4. Hybrid Pipeline

def process_document(
    content: str,
    *,
    llm_client=None,
    confidence_threshold: float = 0.95,
) -> list[ParsedItem]:
    """Full pipeline: regex -> confidence check -> LLM for edge cases."""
    # Step 1: Regex extraction (handles 95-98%)
    items = parse_structured_text(content)

    # Step 2: Confidence scoring
    low_confidence = identify_low_confidence(items, confidence_threshold)

    if not low_confidence or llm_client is None:
        return items


…
अक्सर पूछे जाने वाले सवाल

सब कुछ के लिए सिर्फ़ artificial intelligence (LLM) क्यों न इस्तेमाल करें?

सिर्फ़ AI का इस्तेमाल महंगा और धीमा हो सकता है। यह तरीका ज़्यादा कुशल है, क्योंकि ज़्यादातर काम सरल और तेज़ तरीका करता है, और AI को सबसे मुश्किल मामलों के लिए बचाकर रखा जाता है।

'regex' क्या है?

Regex (regular expression) characters का एक क्रम है जो search pattern बनाता है। यह एक 'recipe' की तरह है, जो किसी बड़े document के अंदर खास texts ढूँढने में मदद करता है।

regex के बिना सिर्फ़ AI (LLM) का इस्तेमाल कब करें?

अगर text में कोई साफ़ pattern नहीं है और वह बहुत अलग-अलग तरह का है (जैसे खुली बातचीत वाली chat), तो अकेला AI सबसे अच्छा विकल्प है।

जानकारी

लेखक का मूल विवरण

Decision framework for parsing structured text (quizzes, forms, invoices, receipts, tables) with a hybrid regex-first pipeline — regex extraction handles 95%+ cheaply, a confidence scorer flags low-confidence items, and an LLM validator fixes only the edge cases. Use when choosing between regex and LLM for text extraction, building a cheap document parser, or optimizing extraction cost and accurac

repo: affaan-m/ECC

लोकप्रियता

GitHub स्टार
280k
रैंकिंग में स्थान
#241
लाइसेंस
MIT
अपडेट की गई
15/10/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

accessibility

280k

यह आपके एजेंट को ऐसे इंटरफ़ेस बनाने में मदद करता है जिन्हें हर कोई इस्तेमाल कर सके। यह पक्का करता है कि वेबसाइट और ऐप दिव्यांग लोगों के लिए भी अच्छे से काम करें, जैसे नेत्रहीन या जिन्हें चलने-फिरने में दिक्कत होती है।

डिज़ाइनइंटरफ़ेस (UI/UX)

agent-architecture-audit

280k

यह आपके एजेंट को जाँचने देता है कि सब कुछ सही से चल रहा है। यह AI सिस्टम में समस्याएँ ढूँढकर ठीक करता है, ताकि वे उम्मीद के मुताबिक काम करें।

AI और एजेंटएजेंट

agent-eval

280k

यह आपके एजेंट को अलग-अलग AI coding tools की तुलना करने में मदद करता है। यह मापता है कि किसी खास काम के लिए कौन सा टूल ज़्यादा तेज़, सस्ता और भरोसेमंद है।

AI और एजेंटएजेंट

agent-harness-construction

280k

यह आपके एजेंट को काम कैसे करना है, यह बेहतर समझने में मदद करता है। यह उन tools और instructions को बेहतर बनाता है जिनका एजेंट इस्तेमाल करता है, ताकि उसकी सफलता की संभावना बढ़े।

AI और एजेंटएजेंट

मिलती-जुलती skills