Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include the latest version of the regex crate in your Cargo.toml
- use regex::Regex;
- fn benchmarker(){
- let regex = match Regex::new(r"\s*(?:(?P<object__0>(?:Benchmarker)))\s*;\s*(?:(?P<action__all>(?:.*?)))\s*; service=\s*(?:(?P<service__all>(?:.*?)))\s*; actual_mps=\s*(?:(?P<actual_mps__int>(?:[+-]? *\d+)))\s*; doable_mps=\s*(?:(?P<doable_mps__int>(?:[+-]? *\d+)))\s*;")
- {
- Ok(r) => r,
- Err(e) => {
- println!("Could not compile regex: {}", e);
- return;
- }
- };
- let string = "2017-09-13_13:20:59.00372 Benchmarker; reporting speed; service=sample_service; actual_mps=0; doable_mps=16;";
- if let Some(captures) = regex.captures(string) {
- for name in regex.capture_names() {
- if let Some(name) = name {
- if let Some(value) = captures.name(name) {
- println!("{} : {}",name,&string[value.start()..value.end()]);
- // hash.insert(name, value);
- }
- }
- }
- // println!("{:?}", captures);
- }
- }
- fn tacs(){
- // let regex = match Regex::new(r"(?uis)TACACS:\s*(?:(?:(?:.*?)))\s*\s*(?:(?P<interface__string>(?:\S+)))\s*\s*(?:(?:(?:\S+)))\s*\s*(?:(?P<action__word>(?:\w+)))\s*\s*(?P<__multipairs0>(?:(?:\S+)\s*(?:=)\s*(?:(?:(?:(?:::ffff:)?(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])|(?:(?:(?:[a-fA-F0-9]{1,4}):){7}(?:[a-fA-F0-9]{1,4})|(?:(?:[a-fA-F0-9]{1,4}):){0,5}(?:[a-fA-F0-9]{1,4})?::(?:(?:[a-fA-F0-9]{1,4}):){0,5}(?:[a-fA-F0-9]{1,4})?)(?:%\d{1,2})?))|(?:[+-]?*\d+)|(?:\w+)|(?:\S+))\s*(?: )\s*)*(?:(?:\S+)\s*(?:=)\s*(?:(?:(?:(?:::ffff:)?(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])|(?:(?:(?:[a-fA-F0-9]{1,4}):){7}(?:[a-fA-F0-9]{1,4})|(?:(?:[a-fA-F0-9]{1,4}):){0,5}(?:[a-fA-F0-9]{1,4})?::(?:(?:[a-fA-F0-9]{1,4}):){0,5}(?:[a-fA-F0-9]{1,4})?)(?:%\d{1,2})?))|(?:[+-]?*\d+)|(?:\w+)|(?:\S+))\s*(?: )?))\s*")
- let regex = match Regex::new(r"TACACS:\s*(?:(?:(?:.*?)))\s*\s*(?:(?P<interface__string>(?:\S+)))\s*\s*(?:(?:(?:\S+)))\s*\s*(?:(?P<action__word>(?:\w+)))\s*\s*(?P<__multipairs0>(?:(?:\S+)\s*(?:=)\s*(?:(?:(?:(?:::ffff:)?(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])|(?:(?:(?:[a-fA-F0-9]{1,4}):){7}(?:[a-fA-F0-9]{1,4})|(?:(?:[a-fA-F0-9]{1,4}):){0,5}(?:[a-fA-F0-9]{1,4})?::(?:(?:[a-fA-F0-9]{1,4}):){0,5}(?:[a-fA-F0-9]{1,4})?)(?:%\d{1,2})?))|(?:[+-]? *\d+)|(?:\w+)|(?:\S+))\s*(?: )\s*)*(?:(?:\S+)\s*(?:=)\s*(?:(?:(?:(?:::ffff:)?(?:(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1?[0-9]?[0-9])|(?:(?:(?:[a-fA-F0-9]{1,4}):){7}(?:[a-fA-F0-9]{1,4})|(?:(?:[a-fA-F0-9]{1,4}):){0,5}(?:[a-fA-F0-9]{1,4})?::(?:(?:[a-fA-F0-9]{1,4}):){0,5}(?:[a-fA-F0-9]{1,4})?)(?:%\d{1,2})?))|(?:[+-]? *\d+)|(?:\w+)|(?:\S+))\s*(?: )?))\s*")
- {
- Ok(r) => r,
- Err(e) => {
- println!("Could not compile regex: {}", e);
- return;
- }
- };
- let string = "<13>Mar 26 06:17:03 kmdinf905e TACACS: 2014-03-26 06:17:03 +0100 kbsr1.capip.kmdnet.dk pix tty2 kmdov3.kmd.dk stop task_id=1419480 start_time=1395811022 timezone=MET service=shell disc-cause=2 disc-cause-ext=1011 connect-progress=101 elapsed_time=0 nas-rx-speed=0 nas-tx-speed=0";
- if let Some(captures) = regex.captures(string) {
- for name in regex.capture_names() {
- if let Some(name) = name {
- if let Some(value) = captures.name(name) {
- println!("{} : {}",name,&string[value.start()..value.end()]);
- // hash.insert(name, value);
- }
- }
- }
- // println!("{:?}", captures);
- }
- }
- fn main() {
- benchmarker();
- let regex = match Regex::new(r"Successful\s*(?P<__key0>\w+)\s*:\s*(?:(?P<__value0__word>\w+))\s* login") {
- Ok(r) => r,
- Err(e) => {
- println!("Could not compile regex: {}", e);
- return;
- }
- };
- let string = "Successful user:Sujan login";
- // let caps = regex.captures(string).unwrap();
- // let dict : HashMap<&str, &str> =
- // regex.capture_names().
- // flatten().
- // filter_map(|n| Some((n, caps.name(n)?.as_str()))).
- // collect();
- // println!("{:#?}", dict);
- if let Some(captures) = regex.captures(string) {
- for name in regex.capture_names() {
- if let Some(name) = name {
- if let Some(value) = captures.name(name) {
- println!("{} : {}",name,&string[value.start()..value.end()]);
- // hash.insert(name, value);
- }
- }
- }
- // println!("{:?}", captures);
- }
- tacs();
- // result will be an iterator over tuples containing the start and end indices for each match in the string
- // let result = regex.captures(string);
- // println!("{}",result)
- // for mat in result {
- // println!("{:?}", mat);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement