Hmbown/CodeWhale · error

bing result regex pattern is valid

Error message

bing result regex pattern is valid

What it means

Panic compiling the Bing result-list pattern `(?is)<li[^>]*class="[^"]*\bb_algo\b[^"]*"[^>]*>(.*?)</li>` in `BING_RESULT_RE.get_or_init`. The literal mixes an inline case-insensitive/dot-matches-newline flag group `(?is)` with a `\b` word boundary inside a raw string; an edit that unbalances the flag group or drops the escaped `\b` to a bare `b` changes semantics, and one that breaks escaping/grouping makes `Regex::new` return `Err`, tripping the expect.

Source

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

}

fn get_snippet_re() -> &'static Regex {
    SNIPPET_RE.get_or_init(|| {
        Regex::new(
            r#"<a[^>]*class=\"result__snippet\"[^>]*>(.*?)</a>|<div[^>]*class=\"result__snippet\"[^>]*>(.*?)</div>"#,
        )
        .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.

View on GitHub (pinned to 0c42157ee5)

Solutions

  1. Fix the literal; double-check `\b` (word boundary) is kept double-backslashed in intent and that every `(` has its `)`.
  2. Validate the pattern standalone: `regex::Regex::new(...).is_ok()` in a scratch test.
  3. Include the getter in the shared compile-all-patterns unit test.

Example fix

// before
Regex::new(r#"(?is)<li[^>]*class=\"[^\"]*\bb_algo\b[^\"]*\"[^>]*>(.*?)</li>"#)
    .expect("bing result regex pattern is valid")

// after: catch a broken edit in CI before it ships
#[test]
fn bing_patterns_compile() {
    assert!(get_bing_result_re().is_match(
        r#"<li class="b_algo""><h2>x</h2></li>"#,
    ));
}
Defensive patterns

Strategy: validation

Validate before calling

#[test]
fn bing_result_pattern_compiles_and_matches() {
    assert!(get_bing_result_re().is_match(
        r#"<li class="b_algo"><h2>x</h2></li>"#,
    ));
}

Try / catch

let results = std::panic::catch_unwind(|| parse_bing_results(&html, max_results))
    .unwrap_or_default();

Prevention

When it happens

Trigger: Editing the Bing selector for a markup change and breaking group balance or the `\b` escapes inside the raw-string literal; the first `parse_bing_results` call after that build panics.

Common situations: Adapting to Bing SERP HTML changes; porting a pattern from a dialect where `\b` or inline flags behave differently; forgetting that inside `r#"..."#` a literal `"` still needs `\"` for the regex, not for Rust.

Related errors


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