regex matches everything for empty string ("") as a pattern #896
-
is it expected behavior that I've checked this docs and there is no info about such behavior there:
lets consider this code: use regex::Regex;
use std::collections::HashSet;
fn get_matches(re_pattern: &str, feed: &[Box<str>]) -> HashSet<String> {
let re = Regex::new(re_pattern).unwrap();
feed.iter()
.filter(|feed_entry| re.is_match(feed_entry))
.map(|x| x.to_string())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_should_ignore_empty_string_as_a_pattern() {
let test_matches = [
Box::from("foo"),
Box::from("bar"),
];
assert_eq!(get_matches("", &test_matches).len(), 0);
}
} it will result in:
tested with [dependencies]
regex = "1.5.5" |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
I think it is expected behavior. Unless the regex has beginning and end markers it will partial match. This is consistent with regex matching in other ecosystems too, for example: https://go.dev/play/p/l4aoWM_sZMI. |
Beta Was this translation helpful? Give feedback.
-
Yeah absolutely this is correct. What do you think the behavior should be? The behavior is solidly correct on both pragmatic and theoretical fronts:
It is possible to write a regex that is the opposite of the empty set, i.e., one that will not match anything. You could write it like so: |
Beta Was this translation helpful? Give feedback.
-
A pattern that matches nothing would be nice if it is not expensive to execute. In this program I am working on, I join a list of valid (sub)patterns with |
Beta Was this translation helpful? Give feedback.
Yeah absolutely this is correct. What do you think the behavior should be?
The behavior is solidly correct on both pragmatic and theoretical fronts:
It is possible to write a regex that is the opposite of the empty set, i.e., one that will not match anything. You could write it like so:
[a&&b]
. Currently though, such patterns won't compile. (I intend to lift that restriction some day.)