| Crates.io | maybe-regex |
| lib.rs | maybe-regex |
| version | 0.2.1 |
| created_at | 2025-04-12 20:35:20.170542+00 |
| updated_at | 2025-04-12 20:58:24.794168+00 |
| description | Wrapper for strings that may be either a regex or a plain-text string |
| homepage | https://github.com/loremdipso/maybe-regex |
| repository | |
| max_upload_size | |
| id | 1631231 |
| size | 30,154 |
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.
// 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);
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();