Skip to content

Instantly share code, notes, and snippets.

@se1983
Created June 30, 2020 17:40
Show Gist options
  • Save se1983/c9217b498b9e6ee17dc29f6c8550a02b to your computer and use it in GitHub Desktop.
Save se1983/c9217b498b9e6ee17dc29f6c8550a02b to your computer and use it in GitHub Desktop.
regex loglines with rust
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