maybe-regex

Crates.iomaybe-regex
lib.rsmaybe-regex
version0.2.1
created_at2025-04-12 20:35:20.170542+00
updated_at2025-04-12 20:58:24.794168+00
descriptionWrapper for strings that may be either a regex or a plain-text string
homepagehttps://github.com/loremdipso/maybe-regex
repository
max_upload_size
id1631231
size30,154
Michael Adams (loremdipso)

documentation

README

maybe-regex

Regex is amazing, but it's also slower than a plain-text search. This is a simple utility that wraps some generic needle, attempts to detect if it looks regex-y, and then provides some useful functions for using it.

Usage

// Regexes work as you'd expect
assert_eq!(MaybeRegex::new("e$").matches("Hello"), false);

// As do plain strings
assert_eq!(MaybeRegex::new("e").matches("Hello"), true);

// Plain string search is case insensitive by default
assert_eq!(MaybeRegex::new("h").matches("Hello"), true);

// ...though that can be disabled
assert_eq!(MaybeRegex::new("h").as_case_sensitive().matches("Hello"), false);

// Strings that start or end with a '-' are understood to be "negative".
// So haystacks that contain the string won't match and vice-versa.
assert_eq!(MaybeRegex::new("-e").matches("Hello"), false);

// You can ignore "negative" behavior by using the 'is_contained_within' method.
assert_eq!(MaybeRegex::new("-e").is_contained_within("Hello"), true);

Performance

It's about what you'd expect, roughly as fast as a regex for regexes or plain strings for plain strings.

The most expensive feature, case insensitivity by default, can be disabled if you'd like:

let needle = MaybeRegex::new("o$").as_case_sensitive();
Commit count: 0

cargo fmt