diff --git a/regex_macros/tests/tests.rs b/regex_macros/tests/tests.rs index ecd965ff8f..217155d221 100644 --- a/regex_macros/tests/tests.rs +++ b/regex_macros/tests/tests.rs @@ -10,6 +10,11 @@ use regex::{Regex, NoExpand}; +#[test] +fn eq() { + assert_eq!(regex!(r"[a-z]+"), Regex::new("[a-z]+").unwrap()); +} + #[test] fn splitn() { let re = regex!(r"\d+"); diff --git a/src/re.rs b/src/re.rs index 845f53a010..2ef58a56eb 100644 --- a/src/re.rs +++ b/src/re.rs @@ -159,6 +159,17 @@ impl fmt::Debug for Regex { } } +/// Equality comparison is based on the original string. It is possible that different regular +/// expressions have the same matching behavior, but are still compared unequal. For example, +/// `\d+` and `\d\d*` match the same set of strings, but are not considered equal. +impl PartialEq for Regex { + fn eq(&self, other: &Regex) -> bool { + self.as_str() == other.as_str() + } +} + +impl Eq for Regex {} + impl FromStr for Regex { type Err = parse::Error;