from __future__ import annotations

import re
from functools import lru_cache


@lru_cache(maxsize=32)
def _get_tag_pattern(tag: str, *, case_insensitive: bool) -> re.Pattern[str]:
    """
    Compile and cache a regex pattern for extracting tag content.
    """
    flags = re.DOTALL | (re.IGNORECASE if case_insensitive else 0)
    return re.compile(rf"<{tag}>(.*?)</{tag}>", flags)


def extract_xml_tag(
    content: str,
    tag: str,
    *,
    case_insensitive: bool = True,
) -> str | None:
    """
    Extract content from an XML-like tag in LLM output.
    """
    pattern = _get_tag_pattern(tag, case_insensitive=case_insensitive)
    match = pattern.search(content)
    return match.group(1) if match else None
