[![regex-badge]][regex] [![lazy_static-badge]][lazy_static] [![cat-text-processing-badge]][cat-text-processing]
验证电子邮件地址的格式是否正确,并提取@
符号之前的所有内容。
#[macro_use]
extern crate lazy_static;
extern crate regex;
use regex::Regex;
fn extract_login(input: &str) -> Option<&str> {
lazy_static! {
static ref RE: Regex = Regex::new(r"(?x)
^(?P<login>[^@\s]+)@
([[:word:]]+\.)*
[[:word:]]+$
").unwrap();
}
RE.captures(input).and_then(|cap| {
cap.name("login").map(|login| login.as_str())
})
}
fn main() {
assert_eq!(extract_login(r"I❤[email protected]"), Some(r"I❤email"));
assert_eq!(
extract_login(r"[email protected]"),
Some(r"sdf+sdsfsd.as.sdsd")
);
assert_eq!(extract_login(r"More@Than@[email protected]"), None);
assert_eq!(extract_login(r"Not an email@email"), None);
}