Email regex invalid #998
-
I'm trying to use the email regex from this SO thread: use once_cell::sync::Lazy;
use regex::Regex;
static EMAIL_ADDRESS_REGEX: Lazy<Regex> = Lazy::new(|| {
Regex::new(
r"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])",
).unwrap()
}); but I'm getting this error:
Can you please help me? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
What you're seeing isn't a regex problem. It's a Rust syntax problem. The issue here is that your regex pattern contains a Here's a link to working program: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8bf265b6ee5f764582b94b9228cc3159 |
Beta Was this translation helpful? Give feedback.
What you're seeing isn't a regex problem. It's a Rust syntax problem. The issue here is that your regex pattern contains a
"
. That needs to be escaped which you can't do in a raw string literal. Instead, you can make use of#
in raw string literals. For example, you can writer#"foo"bar"#
. Things liker##"foo"bar"##
also work. You just have to balance the number of#
.Here's a link to working program: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8bf265b6ee5f764582b94b9228cc3159