assert_matches_regex

Macro assert_matches_regex 

Source
macro_rules! assert_matches_regex {
    ($haystack:expr, $re:expr $(,)?) => { ... };
    ($haystack:expr, $re:expr, $($arg:tt)+) => { ... };
}
Expand description

Asserts that a string matches a regex using regex::Regex.

§Examples

assert_matches_regex!("Hello!", r"(?i)hello");

The haystack can be a String or &str.

assert_matches_regex!(format!("{:?}", vec![1, 2, 3]), r"\[1.*3\]");

let duration = std::time::Duration::from_secs(5);
assert_matches_regex!(duration.as_millis().to_string(), "^50{3}$");

An optional message in the form of a format string can be passed last.

let data = "foo bar";
assert_matches_regex!(data, "^[a-f0-9]$", "expected `{data}` to be a hex string");