Function magic_regexp::create_reg_exp
source · pub fn create_reg_exp(input: impl AsRegex) -> Result<Regex>
Expand description
Returns the regex, which represents the given statement. This is only for convenience and compatibility with magic-regex from npm.
§Example
use magic_regexp::{Digit, Times, create_reg_exp, Exactly, Condition, Text};
use regex::Regex;
const TO_SEARCH: &'static str = "On 2010-03-14, foo happened. On 2014-10-14, bar happened.";
let input = Times(Digit, 4).and(Exactly(Text("-"))).and(Times(Digit, 2)).and(Exactly(Text(("-")))).and(Times(Digit, 2));
assert_eq!(input.to_string(), r"\d{4}-\d{2}-\d{2}");
let input = Times(Digit, 4).grouped_as("year").and(Exactly(Text("-"))).and(Times(Digit, 2).grouped_as("month")).and(Exactly(Text(("-")))).and(Times(Digit, 2).grouped_as("day"));
let re = create_reg_exp(input).unwrap();
assert!(re.is_match("2014-01-01"));
assert_eq!(re.find_iter(TO_SEARCH).count(), 2);
for caps in re.captures_iter(TO_SEARCH) {
// Note that all of the unwraps are actually OK for this regex
// because the only way for the regex to match is if all of the
// capture groups match. This is not true in general though!
println!("year: {}, month: {}, day: {}",
caps.get(1).unwrap().as_str(),
caps.get(2).unwrap().as_str(),
caps.get(3).unwrap().as_str());
}