unclecode/crawl4ai · error · ValueError

Regex for '{label}' won’t compile after fix: {e}

Error message

Regex for '{label}' won’t compile after fix: {e}

What it means

Error "Regex for '{label}' won’t compile after fix: {e}" thrown in unclecode/crawl4ai.

Source

Thrown at crawl4ai/extraction_strategy.py:2552

_WB_FIX = re.compile(r"\x08")               # stray back-space   →   word-boundary
_NEEDS_ESCAPE = re.compile(r"(?<!\\)\\(?![\\u])")   # lone backslash

def _sanitize_schema(schema: Dict[str, str]) -> Dict[str, str]:
    """Fix common JSON-escape goofs coming from LLMs or manual edits."""
    safe = {}
    for label, pat in schema.items():
        # 1️⃣ replace accidental control chars (inc. the infamous back-space)
        pat = _WB_FIX.sub(r"\\b", pat).translate(_CTRL)

        # 2️⃣ double any single backslash that JSON kept single
        pat = _NEEDS_ESCAPE.sub(r"\\\\", pat)

        # 3️⃣ quick sanity compile
        try:
            re.compile(pat)
        except re.error as e:
            raise ValueError(f"Regex for '{label}' won’t compile after fix: {e}") from None

        safe[label] = pat
    return safe


class RegexExtractionStrategy(ExtractionStrategy):
    """
    A lean strategy that finds e-mails, phones, URLs, dates, money, etc.,
    using nothing but pre-compiled regular expressions.

    Extraction returns::

        {
            "url":   "<page-url>",
            "label": "<pattern-label>",
            "value": "<matched-string>",
            "span":  [start, end]
        }

View on GitHub (pinned to 7e80152142)

Solutions

  1. Check that the generated regex for the label is syntactically valid; log the failing pattern and correct or escape the offending constructs.
  2. Provide a hand-written override regex for the label instead of relying on the auto-fix path.

Example fix

strategy = RegexExtractionStrategy(custom={"price": r"\$\d+(?:\.\d{2})?"})

When it happens

Trigger: Thrown at crawl4ai/extraction_strategy.py:2552 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of unclecode/crawl4ai@7e80152142 (2026-08-14). Data as JSON: /api/errors/aef0a23877baf6e0. Report an issue: GitHub.