Enum magic_regexp::Input
source · 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 Input
impl Input
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 Condition for Input
impl Condition for Input
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.
source§impl ToString for Input
impl ToString for Input
source§fn to_string(&self) -> String
fn to_string(&self) -> String
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");