nom_regex::str

Function re_match

Source
pub fn re_match<'a, E>(
    re: Regex,
) -> impl Fn(&'a str) -> IResult<&'a str, &'a str, E>
where E: ParseError<&'a str>,
Expand description

Compares the input with a regular expression and returns the whole input if a match is found.

Requires the regexp feature.

ยงExample

let re = regex::Regex::new(r"^\d{4}").unwrap();
let parser = re_match::<(&str, ErrorKind)>(re);
assert_eq!(parser("2019"), Ok(("", "2019")));
assert_eq!(parser("abc"), Err(Err::Error(("abc", ErrorKind::RegexpMatch))));
assert_eq!(parser("2019-10"), Ok(("", "2019-10")));