// include the latest version of the regex crate in your Cargo.toml
extern crate regex;
use regex::Regex;
fn main() {
let regex = Regex::new(r#"^(?<hostip>[^ ]+) - - \[(?<timestamp>[^ ]+)\s-(?<unknown1>\d+)] "(?<method>[^ ]+) (?<request>[^ ]+) HTTP\/\d\.\d" (?<response>\d+) - (?<bytes>\d+)"#).unwrap();
let string = "10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /iconic/staticContentBuild/js/jquery-1.8.2.js HTTP/1.1\" 200 - 580 SMUSER=- remote-client-ip=10.29.110.51 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257
10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /retail/staticContentBuild/js/pos-module.js HTTP/1.1\" 200 - 120 SMUSER=- remote-client-ip=10.164.96.28 http_referer=- Agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)
10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /retail/mobilepos/screens/ETR2/styles/omnilink.css HTTP/1.1\" 200 - 170 SMUSER=- remote-client-ip=10.27.174.140 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257
10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/staticContentBuild/images/queue_icon_greyw.png HTTP/1.1\" 200 - 230 SMUSER=- remote-client-ip=10.167.154.165 http_referer=- Agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Tablet PC 2.0; .NET CLR 1.1.4322)
10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/staticContentBuild/js/handlebar-data.js HTTP/1.1\" 200 - 140 SMUSER=- remote-client-ip=10.166.12.56 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257
10.144.62.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/mobilepos/screens/ETR2/scripts/templates/device-details/modals/reset-voicemail-password/resetVoicemailPasswordItemView.tmpl HTTP/1.1\" 200 - 180 SMUSER=- remote-client-ip=10.130.4.66 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257
10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /iconic/staticContentBuild/js/accountLines.js HTTP/1.1\" 200 - 240 SMUSER=- remote-client-ip=10.164.18.46 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257
";
// result will be a tuple containing the start and end indices for the first match in the string
let result = regex.captures(string);
let (start, end) = match result {
Some((s, e)) => (s, e),
None => {
// ...
}
};
println!("{}", &string[start, end]);
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Rust, please visit: https://docs.rs/regex/latest/regex/