diff --git a/src/value/attr_value/attr_value_expr.rs b/src/value/attr_value/attr_value_expr.rs index a2663db..add49e4 100644 --- a/src/value/attr_value/attr_value_expr.rs +++ b/src/value/attr_value/attr_value_expr.rs @@ -17,6 +17,7 @@ pub enum BinOp { GT, LT, Elvis, + RegexMatch, } impl std::fmt::Display for BinOp { @@ -34,6 +35,7 @@ impl std::fmt::Display for BinOp { BinOp::GT => write!(f, ">"), BinOp::LT => write!(f, "<"), BinOp::Elvis => write!(f, "?:"), + BinOp::RegexMatch => write!(f, "=~"), } } } @@ -156,6 +158,10 @@ impl AttrValExpr { BinOp::GT => PrimVal::from(a.as_f64()? > b.as_f64()?), BinOp::LT => PrimVal::from(a.as_f64()? < b.as_f64()?), BinOp::Elvis => PrimVal::from(if a.0.is_empty() { b } else { a }), + BinOp::RegexMatch => { + let regex = regex::Regex::new(&b.as_string()?)?; + PrimVal::from(regex.is_match(&a.as_string()?)) + } }) } AttrValExpr::UnaryOp(op, a) => { diff --git a/src/value/attr_value/parser.rs b/src/value/attr_value/parser.rs index bf70b86..efe496e 100644 --- a/src/value/attr_value/parser.rs +++ b/src/value/attr_value/parser.rs @@ -111,6 +111,7 @@ fn parse_term1(i: &str) -> IResult<&str, AttrValExpr, VerboseError<&str>> { map(preceded(tag("!="), parse_term2), |x| (BinOp::NotEquals, x)), map(preceded(tag(">"), parse_term2), |x| (BinOp::GT, x)), map(preceded(tag("<"), parse_term2), |x| (BinOp::LT, x)), + map(preceded(tag("=~"), parse_term2), |x| (BinOp::RegexMatch, x)), )))(i)?; let exprs = remainder.into_iter().fold(initial, |acc, (op, expr)| AttrValExpr::BinOp(box acc, op, box expr));