expression
Regex resource limits
Cap pattern and input lengths before searching; configure compiled expression size. Single searches have a documented worst-case bound proportional to pattern size times input length, but repeated match iteration can have a worse bound. Neither size limits nor this complexity bound guarantee a wall-clock deadline. This example uses the external regex crate; see the staged Cargo manifest. Expected stdout (not executed): Ok(true) Err("pattern error") Err("too large") Common mistake: Treating a regex size setting as a comprehensive resource or time limit.
Formal Syntax & Structure
rust syntaxrust
RegexBuilder::size_limit; input length capsWorking Example
Example Snippetrust
fn main() { fn check(pattern: &str, text: &str) -> Result<bool, &'static str> { if pattern.len() > 64 || text.len() > 128 { return Err("too large"); } let re = regex::RegexBuilder::new(pattern).size_limit(1_000_000).build().map_err(|_| "pattern error")?; Ok(re.is_match(text)) } println!("{:?}", check("cat", "a cat")); println!("{:?}", check("[", "cat")); println!("{:?}", check("cat", &"a".repeat(129)));}