regex_parse

Macro re_parse

re_parse!() { /* proc-macro */ }
Expand description

The main macro of this crate, which parses strings using regular expressions and can extract variables.

§Usage

re_parse!(pattern: StrLiteral, value: &str);

Any variables contained in pattern will be set after the macro has run. For now, the macro will panic if the input cannot be parsed (TODO: Return error)

The pattern is a regular expression which can contain variable captures.

§Variable Captures

  • {var_name}: Captures a single variable of at least one character
  • {var_name*}: Captures multiple (or zero) variables

§Character Classes

re_parse! currently supports these character classes:

  • \s: Any Whitespace (equivalent to [\n\t\r ])
  • \d: Any Digit (equivalent to [0-9])
  • \w: Any Word (equivalent to [a-zA-Z0-0_])

§Example

let name: String;
let score: f32;
re_parse!("The score of {name} is {score}", "The score of user is 55.8");
assert_eq!(name, "user");
assert_eq!(score, 55.8);

§Multiple variables

let temperatures: Vec<f32>;
re_parse!(r"Temperatures: \[({temperatures*}\s*,?\s*)*\]", "Temperatures: [10.0, 9.0, 8.5, 8.0]");
assert_eq!(temperatures, vec![10.0, 9.0, 8.5, 8.0]);

§Efficiency

The macro compiles the pattern into a state-machine which executes in linear time, so it should be very efficient.