Hmbown/CodeWhale · error

bing title regex pattern is valid

Error message

bing title regex pattern is valid

What it means

Panic compiling the Bing result-title pattern `(?is)<h2[^>]*>.*?<a[^>]*href="([^"]+)"[^>]*>(.*?)</a>` in `BING_TITLE_RE.get_or_init`. It depends on two capture groups (URL and anchor text) that downstream code indexes; an edit that removes or reorders groups compiles fine but breaks extraction, while an edit that breaks group/quote balance makes `Regex::new` fail and trips the expect.

Source

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

        .expect("snippet regex pattern is valid")
    })
}

fn get_tag_re() -> &'static Regex {
    TAG_RE.get_or_init(|| Regex::new(r"<[^>]+>").expect("tag regex pattern is valid"))
}

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()))

View on GitHub (pinned to 0c42157ee5)

Solutions

  1. Fix the literal, keeping exactly two capture groups in the order URL-then-anchor.
  2. Pin the group layout in a unit test (`captures_len()`), not just compilation.
  3. Run the Bing scrape tests after the edit.

Example fix

// before
Regex::new(r#"(?is)<h2[^>]*>.*?<a[^>]*href=\"([^\"]+)\"[^>]*>(.*?)</a>"#)
    .expect("bing title regex pattern is valid")

// after: pin both compilation and capture-group layout
#[test]
fn bing_title_pattern_shape() {
    let re = get_bing_title_re();
    assert_eq!(re.captures_len() - 1, 2);
}
Defensive patterns

Strategy: validation

Validate before calling

#[test]
fn bing_title_pattern_shape() {
    let re = get_bing_title_re();
    assert_eq!(re.captures_len() - 1, 2); // URL group, then anchor-text group
}

Try / catch

let titles = std::panic::catch_unwind(|| extract_bing_titles(&html))
    .unwrap_or_default();

Prevention

When it happens

Trigger: Editing the Bing title literal and leaving an unbalanced group or malformed quote escape; the first Bing scrape after that build panics inside the getter.

Common situations: Coping with Bing wrapping titles in extra spans; pasting a pattern where a `"` inside the raw-string/regex double-escaping got mangled.

Related errors


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