RS.CLIPPY.TRIVIAL_REGEX

Trivial regular expressions

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: trivial_regex. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for trivial regex creation (with Regex::new, RegexBuilder::new, or RegexSet::new).

Why is this bad?

Matching the regex can likely be replaced by == or str::starts_with, str::ends_with or std::contains or other str methods.

Known problems

If the same regex is going to be applied to multiple inputs, the precomputations done by Regex construction can give significantly better performance than any of the str-based methods.

Example

Regex::new("^foobar")

Use instead:

str::starts_with("foobar")