vrl/datadog/filter/
regex.rs

1use regex::Regex;
2
3/// Returns compiled word boundary regex.
4///
5/// # Panics
6/// Panics if an invalid wildcard regex is provided.
7#[allow(clippy::module_name_repetitions)] // Renaming is a breaking change.
8#[must_use]
9pub fn word_regex(to_match: &str) -> Regex {
10    Regex::new(&format!(
11        r"\b{}\b",
12        regex::escape(to_match).replace("\\*", ".*")
13    ))
14    .expect("invalid wildcard regex")
15}
16
17/// Returns compiled wildcard regex.
18///
19/// # Panics
20/// Panics if an invalid wildcard regex is provided.
21#[allow(clippy::module_name_repetitions)] // Renaming is a breaking change.
22#[must_use]
23pub fn wildcard_regex(to_match: &str) -> Regex {
24    Regex::new(&format!(
25        "^{}$",
26        regex::escape(to_match).replace("\\*", ".*")
27    ))
28    .expect("invalid wildcard regex")
29}