// 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#"(?s)(?:(?:ok|changed|fatal): \[)([A-Za-z0-9-.]+)(?:\]) => {\s+"msg": \[(.*?)\]\s+}"#).unwrap();
let string = "PLAY [Core Switch] *************************************************************
TASK [Mapping additional variables] ********************************************
ok: [acm-lab-core1.expedient.com]
TASK [Setting the core number] *************************************************
ok: [acm-lab-core1.expedient.com]
TASK [Create directory for configuration files] ********************************
ok: [acm-lab-core1.expedient.com]
TASK [Render the trunk configuration] ******************************************
ok: [acm-lab-core1.expedient.com]
TASK [Render the rollback configuration] ***************************************
ok: [acm-lab-core1.expedient.com]
TASK [register the configuration rendered to a variable3] **********************
changed: [acm-lab-core1.expedient.com]
TASK [debug] *******************************************************************
ok: [acm-lab-core1.expedient.com] => {
\"msg\": [
\"interfaces {\",
\" ae10 {\",
\" unit 1001 {\",
\" encapsulation vlan-bridge;\",
\" vlan-id 1001;\",
\" }\",
\" unit 1002 {\",
\" encapsulation vlan-vpls;\",
\" vlan-id 1002;\",
\" }\",
\" }\",
\" irb {\",
\" unit 1001 {\",
\" description \\\\\\\\\\\"PUBLIC: Dunder Mifflin\\\\\\\\(/)\\\";\",
\" family inet {\",
\" address 206.166.155.2/29 {\",
\" vrrp-group 1001 {\",
\" virtual-address 206.166.155.1;\",
\" priority 200;\",
\" fast-interval 500;\",
\" no-preempt;\",
\" accept-data;\",
\" }\",
\" } \",
\" }\",
\" }\",
\" } \",
\"}\",
\"bridge-domains {\",
\" VLAN1001 {\",
\" vlan-id 1001;\",
\" interface ae10.1001;\",
\" routing-interface irb.1001;\",
\" }\",
\"}\",
\"routing-instances {\",
\" VPLS1002 {\",
\" instance-type vpls;\",
\" vlan-id 1002;\",
\" interface ae10.1002;\",
\" protocols {\",
\" vpls {\",
\" encapsulation-type ethernet-vlan;\",
\" interface ae10.1002;\",
\" no-tunnel-services;vpls-id 1062310;mtu 9216;\",
\" neighbor 206.210.84.1;\",
\" }\",
\" }\",
\" }\",
\"}\",
\"protocols {\",
\" bgp {\",
\" group DunderMiff-vsrx {\",
\" type external;\",
\" import [ tag-vsrx DunderMiff-in ];\",
\" export default-only;\",
\" peer-as 64512;\",
\" neighbor 206.166.155.5;\",
\" }\",
\" }\",
\"}\",
\"policy-options {\",
\" policy-statement DunderMiff-in {\",
\" term customer-networks { \",
\" from {\",
\" route-filter 2.2.1.0/24 exact;\",
\" route-filter 2.2.1.0/24 exact;\",
\" } \",
\" then accept; \",
\" } \",
\" term deny { \",
\" then reject; \",
\" } \",
\" } \",
\"}\"
]
}
PLAY [Core Switch] *************************************************************
TASK [Mapping additional variables] ********************************************
ok: [acm-lab-core2.expedient.com]
TASK [Setting the core number] *************************************************
ok: [acm-lab-core2.expedient.com]
TASK [Create directory for configuration files] ********************************
ok: [acm-lab-core2.expedient.com]
TASK [Render the trunk configuration] ******************************************
ok: [acm-lab-core2.expedient.com]
TASK [Render the rollback configuration] ***************************************
ok: [acm-lab-core2.expedient.com]
TASK [register the configuration rendered to a variable3] **********************
changed: [acm-lab-core2.expedient.com]
TASK [debug] *******************************************************************
ok: [acm-lab-core2.expedient.com] => {
\"msg\": [
\"interfaces {\",
\" ae10 {\",
\" unit 1001 {\",
\" encapsulation vlan-bridge;\",
\" vlan-id 1001;\",
\" }\",
\" unit 1002 {\",
\" encapsulation vlan-vpls;\",
\" vlan-id 1002;\",
\" }\",
\" }\",
\" irb {\",
\" unit 1001 {\",
\" description \\\\\\\\\\\"PUBLIC: Dunder Mifflin\\\\\\\\(/)\\\";\",
\" family inet {\",
\" address 206.166.155.3/29 {\",
\" vrrp-group 1001 {\",
\" virtual-address 206.166.155.1;\",
\" priority 100;\",
\" fast-interval 500;\",
\" no-preempt;\",
\" accept-data;\",
\" }\",
\" } \",
\" }\",
\" }\",
\" } \",
\"}\",
\"bridge-domains {\",
\" VLAN1001 {\",
\" vlan-id 1001;\",
\" interface ae10.1001;\",
\" routing-interface irb.1001;\",
\" }\",
\"}\",
\"routing-instances {\",
\" VPLS1002 {\",
\" instance-type vpls;\",
\" vlan-id 1002;\",
\" interface ae10.1002;\",
\" protocols {\",
\" vpls {\",
\" encapsulation-type ethernet-vlan;\",
\" interface ae10.1002;\",
\" no-tunnel-services;vpls-id 1062310;mtu 9216;\",
\" neighbor 206.210.84.1;\",
\" }\",
\" }\",
\" }\",
\"}\",
\"protocols {\",
\" bgp {\",
\" group DunderMiff-vsrx {\",
\" type external;\",
\" import [ tag-vsrx DunderMiff-in ];\",
\" export default-only;\",
\" peer-as 64512;\",
\" neighbor 206.166.155.5;\",
\" }\",
\" }\",
\"}\",
\"policy-options {\",
\" policy-statement DunderMiff-in {\",
\" term customer-networks { \",
\" from {\",
\" route-filter 2.2.1.0/24 exact;\",
\" route-filter 2.2.1.0/24 exact;\",
\" } \",
\" then accept; \",
\" } \",
\" term deny { \",
\" then reject; \",
\" } \",
\" } \",
\"}\"
]
}
PLAY [Spine Switch] ************************************************************
TASK [Mapping additional variables] ********************************************
ok: [acm-lab-spine1.expedient.com]
TASK [Render the trunk configuration] ******************************************
ok: [acm-lab-spine1.expedient.com]
TASK [Render the rollback configuration] ***************************************
ok: [acm-lab-spine1.expedient.com]
TASK [Display the configuration rendered] **************************************
changed: [acm-lab-spine1.expedient.com]
TASK [debug] *******************************************************************
ok: [acm-lab-spine1.expedient.com] => {
\"msg\": [
\"interfaces {\",
\" ae0 {\",
\" unit 1001 {\",
\" vlan-id 1001;\",
\" }\",
\" unit 1002 {\",
\" vlan-id 1002;\",
\" } \",
\" }\",
\" ae1 {\",
\" unit 1001 {\",
\" vlan-id 1001;\",
\" }\",
\" unit 1002 {\",
\" vlan-id 1002;\",
\" }\",
\" }\",
\"}\",
\"vlans {\",
\" VLAN1001 {\",
\" vlan-id 1001;\",
\" interface ae0.1001;\",
\" interface ae1.1001;\",
\" }\",
\" VLAN1002 {\",
\" vlan-id 1002;\",
\" interface ae0.1002;\",
\" interface ae1.1002;\",
\" } \",
\"}\"
]
}
TASK [No Deploy] ***************************************************************
skipping: [acm-lab-spine1.expedient.com]
TASK [debug] *******************************************************************
skipping: [acm-lab-spine1.expedient.com]
PLAY [POD Switch] **************************************************************
TASK [Setting the interface range] *********************************************
ok: [acm-lab-pod1-sw.expedient.com]
TASK [Mapping additional variables] ********************************************
ok: [acm-lab-pod1-sw.expedient.com]
TASK [Render the trunk configuration] ******************************************
ok: [acm-lab-pod1-sw.expedient.com]
TASK [Render the rollback configuration] ***************************************
ok: [acm-lab-pod1-sw.expedient.com]
TASK [Display the configuration rendered] **************************************
changed: [acm-lab-pod1-sw.expedient.com]
TASK [debug] *******************************************************************
ok: [acm-lab-pod1-sw.expedient.com] => {
\"msg\": [
\"interfaces {\",
\" interface-range DVS {\",
\" unit 0 {\",
\" family ethernet-switching {\",
\" vlan {\",
\" members 1001;\",
\" members 1002;\",
\" members 1111;\",
\" }\",
\" }\",
\" }\",
\" } \",
\" ae0 {\",
\" unit 1001 {\",
\" vlan-id 1001;\",
\" }\",
\" unit 1002 {\",
\" vlan-id 1002;\",
\" } \",
\" }\",
\"}\",
\"vlans {\",
\" VLAN1001 {\",
\" vlan-id 1001;\",
\" interface ae0.1001;\",
\" }\",
\" VLAN1002 {\",
\" vlan-id 1002;\",
\" interface ae0.1002;\",
\" }\",
\" VLAN1111 {\",
\" vlan-id 1111;\",
\" }\",
\"}\"
]
}
PLAY [151-fw - Radius Authentication] ******************************************
TASK [Setting the interface range] *********************************************
ok: [151-fw-mgmt.expedient.com]
TASK [Mapping additional variables] ********************************************
ok: [151-fw-mgmt.expedient.com]
TASK [Render the trunk configuration] ******************************************
changed: [151-fw-mgmt.expedient.com]
TASK [Render the rollback configuration] ***************************************
changed: [151-fw-mgmt.expedient.com]
TASK [Display the configuration rendered] **************************************
changed: [151-fw-mgmt.expedient.com]
TASK [debug] *******************************************************************
ok: [151-fw-mgmt.expedient.com] => {
\"msg\": [
\"security {\",
\" zones {\",
\" security-zone untrust {\",
\" address-book {\",
\" address dunderhead-vsrx 1.1.1.1/32;\",
\" address-set CUSTCBB-Auth-Expedient-Managed-Customer-FW {\",
\" address dunderhead-vsrx;\",
\" }\",
\" }\",
\" }\",
\" }\",
\"}\"
]
}
PLAY [tdp-fw - Radius Authentication] ******************************************
TASK [Setting the interface range] *********************************************
ok: [tdp-fw-mgmt.expedient.com]
TASK [Mapping additional variables] ********************************************
ok: [tdp-fw-mgmt.expedient.com]
TASK [Render the trunk configuration] ******************************************
changed: [tdp-fw-mgmt.expedient.com]
TASK [Render the rollback configuration] ***************************************
changed: [tdp-fw-mgmt.expedient.com]
TASK [Display the configuration rendered] **************************************
changed: [tdp-fw-mgmt.expedient.com]
TASK [debug] *******************************************************************
ok: [tdp-fw-mgmt.expedient.com] => {
\"msg\": [
\"security {\",
\" zones {\",
\" security-zone untrust {\",
\" address-book {\",
\" address dunderhead-vsrx 1.1.1.1/32;\",
\" address-set CUSTCBB-Auth-Expedient-Managed-Customer-FW {\",
\" address dunderhead-vsrx;\",
\" }\",
\" }\",
\" }\",
\" }\",
\"}\"
]
}
PLAY RECAP *********************************************************************
151-fw-mgmt.expedient.com : ok=6 changed=3 unreachable=0 failed=0
acm-lab-core1.expedient.com : ok=7 changed=1 unreachable=0 failed=0
acm-lab-core2.expedient.com : ok=7 changed=1 unreachable=0 failed=0
acm-lab-pod1-sw.expedient.com : ok=6 changed=1 unreachable=0 failed=0
acm-lab-spine1.expedient.com : ok=5 changed=1 unreachable=0 failed=0
tdp-fw-mgmt.expedient.com : ok=6 changed=3 unreachable=0 failed=0 ";
// 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/