// 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#"(?m)(\d.+)(?=\s*\" Unit=\"W\" Type=\"AC_Power\")"#).unwrap();
let string = "<Measurements>
<Measurement Value=\"237.6\" Unit=\"V\" Type=\"AC_Voltage\"/>
<Measurement Value=\"9.828\" Unit=\"A\" Type=\"AC_Current\"/>
<Measurement Value=\"2344.7\" Unit=\"W\" Type=\"AC_Power\"/>
<Measurement Value=\"2343.4\" Unit=\"W\" Type=\"AC_Power_fast\"/>
<Measurement Value=\"50.003\" Unit=\"Hz\" Type=\"AC_Frequency\"/>
<Measurement Value=\"294.4\" Unit=\"V\" Type=\"DC_Voltage1\"/>
<Measurement Value=\"171.3\" Unit=\"V\" Type=\"DC_Voltage2\"/>
<Measurement Value=\"8.056\" Unit=\"A\" Type=\"DC_Current1\"/>
<Measurement Unit=\"A\" Type=\"DC_Current2\"/>
<Measurement Value=\"343.8\" Unit=\"V\" Type=\"LINK_Voltage\"/>
<Measurement Unit=\"W\" Type=\"GridPower\"/>
<Measurement Unit=\"W\" Type=\"GridConsumedPower\"/>
<Measurement Unit=\"W\" Type=\"GridInjectedPower\"/>
<Measurement Unit=\"W\" Type=\"OwnConsumedPower\"/>
<Measurement Value=\"100.0\" Unit=\"%\" Type=\"Derating\"/>
</Measurements>
</Device>
</root>";
// result will be an iterator over tuples containing the start and end indices for each match in the string
let result = regex.captures_iter(string);
for mat in result {
println!("{:?}", mat);
}
}
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/