regex_collection/
common.rs

1use regex::Regex;
2
3/// 火车车次
4///
5/// # Examples
6///
7/// ```
8/// let train_number = String::from("G14");
9/// let train_number_2 = String::from("G11234");
10///
11/// assert_eq!(regex_collection::common::is_train_number(&train_number), true);
12/// assert_eq!(regex_collection::common::is_train_number(&train_number_2), false);
13/// ```
14pub fn is_train_number(train_number: &str) -> bool {
15    let re = Regex::new(r"^[GCDZTSPKXLY1-9]\d{1,4}$").unwrap();
16    re.is_match(train_number)
17}
18
19/// 手机机身码(IMEI)
20///
21/// # Examples
22///
23/// ```
24/// let imei = String::from("122457589068754");
25/// let imei_2 = String::from("G11234");
26///
27/// assert_eq!(regex_collection::common::is_imei(&imei), true);
28/// assert_eq!(regex_collection::common::is_imei(&imei_2), false);
29/// ```
30pub fn is_imei(imei: &str) -> bool {
31    let re = Regex::new(r"^\d{15,17}$").unwrap();
32    re.is_match(imei)
33}
34
35/// 网址
36///
37/// # Examples
38///
39/// ```
40/// let url = String::from("http://baidu.com:8081");
41/// let url_2 = String::from("http://baidu.com/asdf/#/234-123");
42///
43/// assert_eq!(regex_collection::common::is_url(&url), true);
44/// assert_eq!(regex_collection::common::is_url(&url_2), true);
45/// ```
46pub fn is_url(url: &str) -> bool {
47    let re = Regex::new(r"^(((ht|f)tps?)://)?([^!@#$%^&*?.\s-]([^!@#$%^&*?.\s]{0,63}[^!@#$%^&*?.\s])?\.)+[a-z]{2,6}/?").unwrap();
48    re.is_match(url)
49}
50
51/// 必须带端口号的网址(或ip)
52///
53/// # Examples
54///
55/// ```
56/// let url = String::from("http://baidu.com:8081");
57/// let url_2 = String::from("http://baidu.com");
58///
59/// assert_eq!(regex_collection::common::is_url_with_port(&url), true);
60/// assert_eq!(regex_collection::common::is_url_with_port(&url_2), false);
61/// ```
62pub fn is_url_with_port(url: &str) -> bool {
63    let re = Regex::new(r"^((ht|f)tps?://)?[\w-]+(\.[\w-]+)+:\d{1,5}/?$").unwrap();
64    re.is_match(url)
65}
66
67/// 统一社会信用代码
68///
69/// # Examples
70///
71/// ```
72/// let url = String::from("9A1234568378908767");
73/// let url_2 = String::from("http://baidu.com");
74///
75/// assert_eq!(regex_collection::common::is_unified_social_credit_code(&url), true);
76/// assert_eq!(regex_collection::common::is_unified_social_credit_code(&url_2), false);
77/// ```
78pub fn is_unified_social_credit_code(str: &str) -> bool {
79    let re = Regex::new(r"^[0-9A-HJ-NPQRTUWXY]{2}\d{6}[0-9A-HJ-NPQRTUWXY]{10}$").unwrap();
80    re.is_match(str)
81}
82
83/// 视频(video)链接地址(支持swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4格式)
84///
85/// # Examples
86///
87/// ```
88/// let url = String::from("http://ASD.COM/asd/asd");
89/// let url_2 = String::from("http://baidu.com/ewe.mov");
90///
91/// assert_eq!(regex_collection::common::is_video_url(&url), false);
92/// assert_eq!(regex_collection::common::is_video_url(&url_2), true);
93/// ```
94pub fn is_video_url(url: &str) -> bool {
95    let re =
96        Regex::new(r"^https?://(.+/)+.+(\.(swf|avi|flv|mpg|rm|mov|wav|asf|3gp|mkv|rmvb|mp4))$")
97            .unwrap();
98    re.is_match(url)
99}
100
101/// 图片(video)链接地址(支持gif|png|jpg|jpeg|webp|svg|psd|bmp|tif格式)
102///
103/// # Examples
104///
105/// ```
106/// let url = String::from("http://ASD.COM/asd/asd.psd");
107/// let url_2 = String::from("https://baidu.com/ewe.jpg");
108///
109/// assert_eq!(regex_collection::common::is_image_url(&url), false);
110/// assert_eq!(regex_collection::common::is_image_url(&url_2), true);
111/// ```
112pub fn is_image_url(url: &str) -> bool {
113    let re = Regex::new(r"^https?://(.+/)+.+(gif|png|jpg|jpeg|webp|svg|psd|bmp|tif)$").unwrap();
114    re.is_match(url)
115}
116
117/// base64格式
118///
119/// # Examples
120///
121/// ```
122/// let text = String::from("http://ASD.COM/asd/asd.psd");
123/// let text_2 = String::from("data:image/gif;base64,dsafasdfasddGhpcyBpcyBhIGV4YW1wbGU=");
124///
125/// assert_eq!(regex_collection::common::is_base64(&text), false);
126/// assert_eq!(regex_collection::common::is_base64(&text_2), true);
127/// ```
128pub fn is_base64(url: &str) -> bool {
129    let re = Regex::new(r"^\s*data:(?:[a-z]+/[a-z0-9-+.]+(?:;[a-z-]+=[a-z0-9-]+)?)?(?:;base64)?,([a-z0-9!$&',()*+;=\-._~:@/?%\s]*?)\s*$").unwrap();
130    re.is_match(url)
131}
132
133/// 银行卡号
134///
135/// # Examples
136///
137/// ```
138/// let text = String::from("http://ASD.COM/asd/asd.psd");
139/// let text_2 = String::from("data:image/gif;base64,dsafasdfasddGhpcyBpcyBhIGV4YW1wbGU=");
140///
141/// assert_eq!(regex_collection::common::is_credit_card_numbers(&text), false);
142/// assert_eq!(regex_collection::common::is_credit_card_numbers(&text_2), true);
143/// ```
144pub fn is_credit_card_numbers(url: &str) -> bool {
145    let re = Regex::new(r"^[1-9]\d{9,29}$").unwrap();
146    re.is_match(url)
147}