1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use once_cell::sync::Lazy;
pub use regex::*;

/// 激活码正则
pub static REGEX_ACTIVE_CODE: &'static str = r"^[A-Za-z0-9-]{29}$";
/// IPV4正则
pub static REGEX_IPV4: &'static str = r"^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$";
/// URL正则
pub static REGEX_URL: &'static str = r"^(https?://)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*/?$";
/// 邮箱正则
pub static REGEX_EMAIL: &'static str = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$";
/// MAC正则
pub static REGEX_MAC: &'static str =
  r"[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}-[0-9A-Fa-f]{2}";

/// 正则表达式,用于匹配格式化字符串中的占位符
pub static REGEX_FORMAT_PATTERN: Lazy<Regex> = Lazy::new(|| {
  Regex::new(r"\{([a-zA-Z0-9_]+)\}").expect("Invalid regex pattern for REGEX_FORMAT_PATTERN")
});

#[cfg(target_os = "windows")]
/// Windows环境下环境变量的正则表达式
pub static REGEX_ENV_PATTERN: Lazy<Regex> = Lazy::new(|| {
  Regex::new(r"\%([a-zA-Z0-9_]+)\%")
    .expect("Invalid regex pattern for REGEX_ENV_PATTERN on Windows")
});

#[cfg(any(target_os = "linux", target_os = "macos"))]
/// Linux和macOS环境下环境变量的正则表达式
pub static REGEX_ENV_PATTERN: Lazy<Regex> = Lazy::new(|| {
  Regex::new(r"\$\{([a-zA-Z0-9_]+)\}")
    .expect("Invalid regex pattern for REGEX_ENV_PATTERN on Unix-like systems")
});

#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
/// 对于其他操作系统的环境变量正则表达式
pub static REGEX_ENV_PATTERN: Lazy<Regex> = Lazy::new(|| {
  Regex::new(r"\$([a-zA-Z0-9_]+)")
    .expect("Invalid regex pattern for REGEX_ENV_PATTERN on other systems")
});

///
fn wildcard_to_regex_for_path(pattern: &str) -> Result<Regex, regex::Error> {
  let mut regex_pattern = String::new();
  let main_separator = std::path::MAIN_SEPARATOR.to_string();
  let mut chars = pattern.chars().peekable();

  while let Some(c) = chars.next() {
    match c {
      '*' => regex_pattern.push_str(&format!(r"([^{}]*?)", regex::escape(&main_separator))),
      '?' => regex_pattern.push('.'),
      '.' | '$' | '^' | '+' | '|' | '[' | ']' | '(' | ')' | '\\' => {
        regex_pattern.push_str(&regex::escape(&c.to_string()));
      }
      _ => regex_pattern.push(c),
    }
  }

  // 创建区分大小写的正则表达式
  Regex::new(&regex_pattern)
}

/// # 正则匹配2
/// ```rust
/// use e_utils::{fs, regex::regex2};
/// fn main() {
///   let base_dir = "D:\\keys"; // 修改为您的测试目录
///   let patterns = vec!["*.sha256","*certkey.*"];
///   for p in fs::tree_folder(base_dir).unwrap() {
///     for pattern in &patterns {
///       println!(
///         "File {} matching pattern '{}' -> {}:",
///         p,
///         pattern,
///         regex2(&p, pattern)
///       );
///     }
///   }
/// }
/// ```
pub fn regex2(input: &str, pattern: &str) -> bool {
  wildcard_to_regex_for_path(pattern)
    .and_then(|x| Ok(x.is_match(input)))
    .unwrap_or(false)
}
#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_matches_path_pattern() {
    assert!(regex2("C:/test/example.txt", "*.txt"));
    assert!(regex2("D:\\1hello2.json", "1*2.json"));
    assert!(regex2("test.txt", "*est.txt"));
    assert!(!regex2("test.txt", "est.txt"));
    assert!(!regex2("no_match", "*.txt"));
  }
}