File tree 1 file changed +31
-0
lines changed
1 file changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -21,12 +21,43 @@ pub struct RegexPredicate {
21
21
re : regex:: Regex ,
22
22
}
23
23
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
+
24
41
impl Predicate < str > for RegexPredicate {
25
42
fn eval ( & self , variable : & str ) -> bool {
26
43
self . re . is_match ( variable)
27
44
}
28
45
}
29
46
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
+ }
30
61
/// Creates a new `Predicate` that uses a regular expression to match the string.
31
62
///
32
63
/// # Examples
You can’t perform that action at this time.
0 commit comments