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§
Implementations§
source§impl<'a> Input<'a>
impl<'a> Input<'a>
sourcepub fn grouped_as(&self, name: &str) -> Regex
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!(®ex.captures("1").unwrap()["digits"], "1");
sourcepub fn as(&self, name: &str) -> Regex
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
.
sourcepub fn grouped(&self) -> Regex
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!(®ex.captures("1 5").unwrap()[1], "1");
assert_eq!(®ex.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!(®ex.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 = ®ex
.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> Condition for Input<'a>
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
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
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
fn optionally(self) -> Regex
Returns the regex, which sets the given statement to optional.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more