Skip to content

Commit 4b5bcdf

Browse files
committed
feat(str): Add regex repetition count
Fixes #27
1 parent 034edfd commit 4b5bcdf

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/str/regex.rs

+31
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,43 @@ pub struct RegexPredicate {
2121
re: regex::Regex,
2222
}
2323

24+
impl RegexPredicate {
25+
/// Require a specific count of matches.
26+
///
27+
/// # Examples
28+
///
29+
/// ```
30+
/// use predicates::prelude::*;
31+
///
32+
/// let predicate_fn = predicate::str::is_match("T[a-z]*").unwrap().count(3);
33+
/// assert_eq!(true, predicate_fn.eval("One Two Three Two One"));
34+
/// assert_eq!(false, predicate_fn.eval("One Two Three"));
35+
/// ```
36+
pub fn count(self, count: usize) -> RegexMatchesPredicate {
37+
RegexMatchesPredicate { re: self.re, count }
38+
}
39+
}
40+
2441
impl Predicate<str> for RegexPredicate {
2542
fn eval(&self, variable: &str) -> bool {
2643
self.re.is_match(variable)
2744
}
2845
}
2946

47+
/// Predicate that checks for repeated patterns.
48+
///
49+
/// This is created by `predicates::str::is_match(...).count`.
50+
#[derive(Clone, Debug)]
51+
pub struct RegexMatchesPredicate {
52+
re: regex::Regex,
53+
count: usize,
54+
}
55+
56+
impl Predicate<str> for RegexMatchesPredicate {
57+
fn eval(&self, variable: &str) -> bool {
58+
self.re.find_iter(variable).count() == self.count
59+
}
60+
}
3061
/// Creates a new `Predicate` that uses a regular expression to match the string.
3162
///
3263
/// # Examples

0 commit comments

Comments
 (0)