Skip to content

Commit f2bc2f6

Browse files
author
Samy Kacimi
committed
add URL support
1 parent 54b3cae commit f2bc2f6

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

‎README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ let filtered_ips = commonregex::ip::v4(&log);
3030
commonregex::internet::email(&text)
3131
```
3232

33+
**URL**
34+
35+
```rust
36+
commonregex::internet::url(&text)
37+
```
38+
3339
### IP Addresses
3440

3541
**IPv4**

‎src/commonregex/internet.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ pub fn email(text: &String) -> Vec<&str> {
2222
matcher::match_results(text, regex_drawer::internet::EMAIL)
2323
}
2424

25+
/// Returns matched URLs as a vector of strings
26+
///
27+
/// # Arguments
28+
///
29+
/// * `text` - A String representing the text in which to search.
30+
///
31+
/// # Examples
32+
///
33+
/// ```
34+
/// extern crate commonregex_rs;
35+
///
36+
/// use commonregex_rs::commonregex;
37+
///
38+
/// let text = String::from("Contribute at https://github.com/fakenine/commonregex-rs !");
39+
///
40+
/// assert_eq!(vec!["https://github.com/fakenine/commonregex-rs"], commonregex::internet::url(&text));
41+
/// ```
42+
pub fn url(text: &String) -> Vec<&str> {
43+
matcher::match_results(text, regex_drawer::internet::URL)
44+
}
45+
46+
2547
#[cfg(test)]
2648
mod tests {
2749
use super::*;

‎src/commonregex/regex_drawer.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod internet {
22
pub const EMAIL: &'static str = r#"(?i)([A-Za-z0-9!#$%&'*+/=?^_{|.}~-]+@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)"#;
3+
pub const URL: &'static str = r#"(?:(?:https?://)?(?:[a-z0-9.\-]+|www|[a-z0-9.\-])[.](?:[^\s()<>]+|\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s!()\[\]{};:'".,<>?]))"#;
34
}
45

56
pub mod ip {

0 commit comments

Comments
 (0)