Created
June 30, 2020 17:40
-
-
Save se1983/c9217b498b9e6ee17dc29f6c8550a02b to your computer and use it in GitHub Desktop.
regex loglines with rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use regex::Regex; | |
fn main() { | |
let datetime = r"[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9],[0-9][0-9][0-9]"; | |
let host = r"([^\s]+)"; | |
let service = r"([^\s]+)"; | |
let message = r"(.*)"; | |
let re = Regex::new(&format!("(?P<datetime>({d})) (?P<hostname>({h}))] (?P<service>({s})) (?P<message>({m}))", | |
d = datetime, h = host, s = service, m = message)).unwrap(); | |
let log_line = "[2006-02-08 22:20:02,165 192.168.0.1] fbloggs Protocol problem: connection reset"; | |
let date = re.captures(log_line).unwrap(); | |
println!("{:?}", &date) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment