File tree Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Expand file tree Collapse file tree 1 file changed +31
-0
lines changed Original file line number Diff line number Diff 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+
2441impl 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
You can’t perform that action at this time.
0 commit comments