Regex vs LLM for Structured Text Parsing
A practical decision framework for parsing structured text: regex first, LLM only for edge cases. Detalhes em references/.
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
Core Principles
- Regex first — handles 95-98% cheaply and deterministically
- Confidence gate at 0.95 — only low-confidence items go to LLM
- Cheapest LLM for validation (Haiku-class is enough)
- Never mutate — return new instances from cleaning/validation
- Log metrics — regex rate and LLM call count track health
Example
items = parse_structured_text(content)
low = identify_low_confidence(items, 0.95)
result = process_document(content, llm_client=client)
References
references/decision-architecture.md— decision tree, pipeline diagram, use casesreferences/regex-parser.md— regex parser code with ParsedItemreferences/confidence-llm.md— confidence scoring and LLM validatorreferences/pipeline-practices.md— hybrid pipeline, metrics, best/anti-patterns
Checklist
- Format is consistent/repeating (>90%) before choosing regex
- Confidence threshold set (default 0.95) with flagged reasons
- LLM only on low-confidence items, cheapest model
- No mutation of parsed objects; edge cases tested