Hmbown/CodeWhale · error

bing snippet regex pattern is valid

Error message

bing snippet regex pattern is valid

What it means

Panic compiling the Bing caption/snippet pattern `(?is)<div[^>]*class="[^"]*\bb_caption\b[^"]*"[^>]*>.*?<p[^>]*>(.*?)</p>` in `BING_SNIPPET_RE.get_or_init`. Same family as the other scrape getters: a hardcoded literal whose only failure mode is a manual edit breaking regex syntax (flag group, `\b` escapes, group balance), which makes `Regex::new` return `Err` and trips the expect on first use.

Source

Thrown at crates/tui/src/tools/web/scrape.rs:68

fn get_bing_result_re() -> &'static Regex {
    BING_RESULT_RE.get_or_init(|| {
        Regex::new(r#"(?is)<li[^>]*class=\"[^\"]*\bb_algo\b[^\"]*\"[^>]*>(.*?)</li>"#)
            .expect("bing result regex pattern is valid")
    })
}

fn get_bing_title_re() -> &'static Regex {
    BING_TITLE_RE.get_or_init(|| {
        Regex::new(r#"(?is)<h2[^>]*>.*?<a[^>]*href=\"([^\"]+)\"[^>]*>(.*?)</a>"#)
            .expect("bing title regex pattern is valid")
    })
}

fn get_bing_snippet_re() -> &'static Regex {
    BING_SNIPPET_RE.get_or_init(|| {
        Regex::new(r#"(?is)<div[^>]*class=\"[^\"]*\bb_caption\b[^\"]*\"[^>]*>.*?<p[^>]*>(.*?)</p>"#)
            .expect("bing snippet regex pattern is valid")
    })
}

/// Parse DuckDuckGo HTML SERP results. Known spam-domain hits are omitted.
pub fn parse_duckduckgo_results(html: &str, max_results: usize) -> Vec<ScrapedSearchResult> {
    let title_re = get_title_re();
    let snippet_re = get_snippet_re();
    let snippets: Vec<String> = snippet_re
        .captures_iter(html)
        .filter_map(|cap| cap.get(1).or_else(|| cap.get(2)))
        .map(|m| normalize_text(m.as_str()))
        .collect();

    let mut results = Vec::new();
    for (idx, cap) in title_re.captures_iter(html).enumerate() {
        if results.len() >= max_results {
            break;
        }

View on GitHub (pinned to 0c42157ee5)

Solutions

  1. Fix the literal and validate it compiles standalone before committing.
  2. Add the getter to the shared compile-all-patterns unit test.
  3. Run `cargo test -p codewhale-tui --lib scrape` after any pattern change.

Example fix

// before
Regex::new(
    r#"(?is)<div[^>]*class=\"[^\"]*\bb_caption\b[^\"]*\"[^>]*>.*?<p[^>]*>(.*?)</p>"#,
).expect("bing snippet regex pattern is valid")

// after: guarded initialization in the shared test
#[test]
fn all_bing_snippet_edits_compile() {
    let _ = get_bing_snippet_re();
}
Defensive patterns

Strategy: validation

Validate before calling

#[test]
fn bing_snippet_pattern_compiles() {
    let _ = get_bing_snippet_re();
}

Try / catch

let snippets = std::panic::catch_unwind(|| extract_bing_snippets(&html))
    .unwrap_or_default();

Prevention

When it happens

Trigger: Editing the caption literal for a Bing markup change and introducing a syntax error; the first Bing snippet extraction after that build panics inside `get_bing_snippet_re`.

Common situations: Bing markup variants where the caption div or inner `<p>` structure changes; hand-merging a pattern fix across branches and losing an escape.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@0c42157ee5 (2026-08-20). Data as JSON: /api/errors/41e428c590a335d8. Report an issue: GitHub.