Enum magic_regexp::Input

source ·
pub enum Input<'a> {
    OneOrMore(Type<'a>),
    Exactly(Type<'a>),
    Maybe(Type<'a>),
    Times(Type<'a>, usize),
}
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<'a>)

§

Exactly(Type<'a>)

§

Maybe(Type<'a>)

§

Times(Type<'a>, usize)

Implementations§

source§

impl<'a> Input<'a>

source

pub fn grouped_as(&self, name: &str) -> Regex

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");
source

pub fn as(&self, name: &str) -> Regex

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

source

pub fn grouped(&self) -> Regex

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§

source§

impl<'a> AsRegex for Input<'a>

source§

fn as_regex(&self) -> Result<Regex>

Returns the regex, which represents the wanted statement.
source§

impl<'a> Condition for Input<'a>

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"));
source§

fn and(self, other: impl AsRegex) -> Regex

Returns the regex, which chains the two given statements with an and condition.
source§

fn or(self, other: impl AsRegex) -> Regex

Returns the regex, which chains the two given statements with an or condition.
source§

fn optionally(self) -> Regex

Returns the regex, which sets the given statement to optional.
source§

impl<'a> Display for Input<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Input<'a>

§

impl<'a> RefUnwindSafe for Input<'a>

§

impl<'a> Send for Input<'a>

§

impl<'a> Sync for Input<'a>

§

impl<'a> Unpin for Input<'a>

§

impl<'a> UnwindSafe for Input<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.