Crate magic_regexp

source ·
Expand description

This crate provides a library for creating regular expressions in a more readable way. It stands on the shoulders of the regex crate.

For more specific details on the API for regular expressions, please see around the enums in the left sidebar.

Example: Find a date

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("-".to_string()))).and(Times(Digit, 2)).and(Exactly(Text(("-".to_string())))).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("-".to_string()))).and(Times(Digit, 2).grouped_as("month")).and(Exactly(Text(("-".to_string())))).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());
}

Re-exports

  • pub use type::Input::*;
  • pub use type::Type::*;

Enums

  • An error, which can occur while using this crate. Mostly used to wrap errors from the Regex crate.
  • This is a regex input that can be used to match a single character or a group of characters. Can be used to create a regex that matches a single character or a group of characters. For example, Input::Exactly(Type::Digit) will match a single digit.
  • Represents a regex type. This enum is used to create the smallest regex statement. For example, Type::Digit will create the regex \d.

Traits

  • A trait, which allows to convert something to a regex. Mostly needed to work with this lib and Regex crate.
  • A trait, which allows to chain regex statements with conditions. Import this, if you want to use the and, or and optionally methods and chain statements.

Functions

  • Returns the regex, which represents the given statement. This is only for convenience and compatibility with magic-regex from npm.
  • Returns the opposite of the given type. For example, Type::Digit will return Type::NotDigit. Returns the same type if it is not a type that can be negated.

Type Definitions

  • A type, which is used to return results from this crate. Mostly used to wrap results from the Regex crate.