Enum magic_regexp::Input

source ·
pub enum Input {
    OneOrMore(Type),
    Exactly(Type),
    Maybe(Type),
    Times(Typeusize),
}
Expand description

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.

Example

use magic_regexp::{create_reg_exp, Input, Type};

let regex = create_reg_exp(Input::Exactly(Type::Digit)).unwrap();
assert!(regex.is_match("1"));
assert!(!regex.is_match("12"));
assert!(regex.is_match("1 2"));

Example

use magic_regexp::{create_reg_exp, Input, Type};

let regex = create_reg_exp(Input::OneOrMore(Type::Digit)).unwrap();
assert!(regex.is_match("1"));
assert!(regex.is_match("12"));
assert!(regex.is_match("1 2"));

Example

use magic_regexp::{create_reg_exp, Input, Type};

let regex = create_reg_exp(Input::Maybe(Type::Digit)).unwrap();
assert!(regex.is_match("1"));
assert!(regex.is_match(""));
assert!(regex.is_match("12"));
assert!(regex.is_match("a"));
assert!(regex.is_match("1 2"));

Variants§

§

OneOrMore(Type)

§

Exactly(Type)

§

Maybe(Type)

§

Times(Typeusize)

Implementations§

This defines the entire input so far as a named capture group.

Example
use magic_regexp::{create_reg_exp, Condition, Exactly, Digit, LetterLowercase, OneOrMore};

let regex = create_reg_exp(OneOrMore(Digit).grouped_as("digits")).unwrap();
assert_eq!(&regex.captures("1").unwrap()["digits"], "1");

This defines the entire input so far as a named capture group. This is an alias for grouped_as.

This defines the entire input so far as an anonymous group.

Example
use magic_regexp::{create_reg_exp, Condition, Exactly, Digit, LetterLowercase, OneOrMore, Type, Char, Whitespace, Maybe, Options};
use regex::Regex;

let regex = create_reg_exp(OneOrMore(Digit).grouped()
    .and(Exactly(Whitespace))
    .and(OneOrMore(Char).or(Exactly(Whitespace)).optionally())
    .and(OneOrMore(Digit).grouped())
).unwrap();
assert_eq!(&regex.captures("1 5").unwrap()[1], "1");
assert_eq!(&regex.captures("134 23").unwrap()[1], "134");
// The folloing example is not really useful, because it shows only the first match.
// The second match is not captured. See the next example for a more useful example.
assert_eq!(&regex.captures("this is the 134 test 213").unwrap()[1], "134");
// This is a bit more complex, because we are using anonymous groups in regex.
let cap = &regex
    .find_iter("multiple numbers 134 2123")
    .filter_map(|digits| digits.as_str().parse().ok())
    .collect::<Vec<String>>()
    .join(" ");
let expected = ["134", "2123"].join(" ");
assert_eq!(cap, &expected);

Trait Implementations§

Returns the regex, which represents the wanted statement.

Returns a Regex, which chains the 2 given regexes with an and operator.

Example

use magic_regexp::{create_reg_exp, Condition, Exactly, Digit, LetterLowercase};

let regex = create_reg_exp(Exactly(Digit).or(Exactly(LetterLowercase))).unwrap();
assert!(regex.is_match("1"));
assert!(regex.is_match("a"));
assert!(!regex.is_match("A"));
assert!(!regex.is_match("12"));
assert!(!regex.is_match("1a"));
assert!(regex.is_match("1 a"));
Returns the regex, which chains the two given statements with an and condition.
Returns the regex, which chains the two given statements with an or condition.
Returns the regex, which sets the given statement to optional.

Returns a string representation of the input. For example, Input::Exactly(Type::Digit) will return \d.

Example
use magic_regexp::{Exactly, Digit, AsRegex, create_reg_exp};

let regex = create_reg_exp(Exactly(Digit)).unwrap();
assert!(regex.is_match("1"));
assert!(!regex.is_match("12"));
Example
use magic_regexp::{Input, Type};

let input = Input::Exactly(Type::Text("abc".into()));
assert_eq!(input.to_string(), "abc");
let input = Input::Exactly(Type::Text(".".to_string()));
assert_eq!(input.to_string(), r"\.");
Example
use magic_regexp::{not, OneOrMore, Options};
use regex::Regex;

let input = OneOrMore(not(Options("01".to_string())));
assert_eq!(input.to_string(), r"([^01]+)");
let re = Regex::new(&input.to_string()).unwrap();
assert_eq!(re.replace("1078910", ""), "1010");

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.