| Crates.io | regex-specificity |
| lib.rs | regex-specificity |
| version | 0.1.0 |
| created_at | 2025-12-29 14:24:12.386658+00 |
| updated_at | 2025-12-31 09:38:14.444599+00 |
| description | A heuristic-based crate to calculate the specificity of a regular expression pattern against a specific string. |
| homepage | https://github.com/KKW557/regex-specificity |
| repository | https://github.com/KKW557/regex-specificity |
| max_upload_size | |
| id | 2010669 |
| size | 14,269 |
A heuristic-based crate to calculate the specificity of a regular expression pattern against a specific string.
Specificity measures how "precise" a match is. For example, the pattern abc is more specific to the string "abc" than the pattern a.c or .*.
The calculation follows these principles:
[a-z]) specificity higher than broader ones (e.g., .).The get function assumes that the string provided is already a full match for the pattern.
If the pattern does not match the string, the resulting specificity will be mathematically inconsistent and meaningless for comparison purposes.
let string = "abc";
let high = get(string, "abc").unwrap();
let low = get(string, ".*").unwrap();
assert!(high > low);
Since this crate uses a greedy heuristic based on the HIR (High-level Intermediate Representation), certain patterns may yield the same specificity even if they look different.
A common example is when a wildcard .* "swallows" the entire string before other parts of the pattern can be evaluated.
let string = "cat";
let pattern1 = r".*";
let pattern2 = r".*a.*";
assert_eq!(
get(string, pattern1).unwrap(),
get(string, pattern2).unwrap()
)
If you need to distinguish between patterns with identical specificity, we recommend using the pattern length as a secondary tie-breaker:
if result_a == result_b {
return pattern_a.len().cmp(&pattern_b.len());
}
This project is licensed under the MIT License © 2025 557.