Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. // include the latest version of the regex crate in your Cargo.toml
  2. use regex::Regex;
  3.  
  4. fn benchmarker(){
  5. 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*;")
  6. {
  7. Ok(r) => r,
  8. Err(e) => {
  9. println!("Could not compile regex: {}", e);
  10. return;
  11. }
  12. };
  13. let string = "2017-09-13_13:20:59.00372 Benchmarker; reporting speed; service=sample_service; actual_mps=0; doable_mps=16;";
  14. if let Some(captures) = regex.captures(string) {
  15. for name in regex.capture_names() {
  16. if let Some(name) = name {
  17. if let Some(value) = captures.name(name) {
  18. println!("{} : {}",name,&string[value.start()..value.end()]);
  19. // hash.insert(name, value);
  20. }
  21. }
  22. }
  23. // println!("{:?}", captures);
  24. }
  25. }
  26.  
  27. fn tacs(){
  28. // 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*")
  29. 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*")
  30. {
  31. Ok(r) => r,
  32. Err(e) => {
  33. println!("Could not compile regex: {}", e);
  34. return;
  35. }
  36. };
  37. 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";
  38. if let Some(captures) = regex.captures(string) {
  39. for name in regex.capture_names() {
  40. if let Some(name) = name {
  41. if let Some(value) = captures.name(name) {
  42. println!("{} : {}",name,&string[value.start()..value.end()]);
  43. // hash.insert(name, value);
  44. }
  45. }
  46. }
  47. // println!("{:?}", captures);
  48. }
  49. }
  50.  
  51.  
  52. fn main() {
  53. benchmarker();
  54. let regex = match Regex::new(r"Successful\s*(?P<__key0>\w+)\s*:\s*(?:(?P<__value0__word>\w+))\s* login") {
  55. Ok(r) => r,
  56. Err(e) => {
  57. println!("Could not compile regex: {}", e);
  58. return;
  59. }
  60. };
  61. let string = "Successful user:Sujan login";
  62. // let caps = regex.captures(string).unwrap();
  63. // let dict : HashMap<&str, &str> =
  64. // regex.capture_names().
  65. // flatten().
  66. // filter_map(|n| Some((n, caps.name(n)?.as_str()))).
  67. // collect();
  68. // println!("{:#?}", dict);
  69.  
  70.  
  71. if let Some(captures) = regex.captures(string) {
  72. for name in regex.capture_names() {
  73. if let Some(name) = name {
  74. if let Some(value) = captures.name(name) {
  75. println!("{} : {}",name,&string[value.start()..value.end()]);
  76. // hash.insert(name, value);
  77. }
  78. }
  79. }
  80. // println!("{:?}", captures);
  81. }
  82. tacs();
  83. // result will be an iterator over tuples containing the start and end indices for each match in the string
  84. // let result = regex.captures(string);
  85. // println!("{}",result)
  86. // for mat in result {
  87. // println!("{:?}", mat);
  88. // }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement