// 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)(?P<query_date>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z UTC) \[ db=(?P<db>\w+) LOG:(?P<query_text>[\w|\W]*?;)").unwrap();
let string = "'2021-06-23T08:02:08Z UTC [ db=dev LOG: BEGIN;
'2021-06-23T08:02:08Z UTC [ db=dev LOG: SET datestyle TO ISO;
'2021-06-23T08:02:08Z UTC [ db=dev LOG: SET TRANSACTION READ ONLY;
'2021-06-23T08:02:08Z UTC [ db=dev LOG: SET STATEMENT_TIMEOUT
TO 300000;
'2021-06-23T08:02:08Z UTC [ LOG: /* hash: 8d9692aa66628f2ea5b0b9de8e4ea59b */
SELECT action,
status,
COUNT(*) AS num_req
FROM stl_datashare_changes_consumer
WHERE actiontime > getdate() - INTERVAL '1 day'
GROUP BY 1,2;
'2021-06-23T08:02:08Z UTC [ LOG: SELECT pg_catalog.stll_datashare_changes_consumer.action AS action, pg_catalog.stll_datashare_changes_consumer.status AS status, COUNT(*) AS num_req FROM pg_catalog.stll_datashare_changes_consumer WHERE pg_catalog.stll_datashare_changes_consumer.actiontime > getdate() - interval '1 day'::Interval GROUP BY 1, 2;
'2021-06-23T08:02:08Z UTC [ LOG: COMMIT;
'2021-06-23T08:02:08Z UTC [ LOG: SET query_group to ''
'2021-06-23T08:02:22Z UTC [ LOG: SELECT 1
'2021-06-23T08:02:30Z UTC [ LOG: /* hash: 64f5dca78e917617f51632257854cb2f */
WITH per_commit_info AS
(
SELECT date_trunc('day', startwork) AS day,
c.xid,
SUM(num_metadata_blocks_retained) AS sum_retained,
SUM(total_metadata_blocks) AS sum_total,
AVG(num_metadata_blocks_retained) AS avg_retained,
AVG(total_metadata_blocks) AS avg_total
FROM stl_commit_stats c,
stl_commit_internal_stats i
WHERE c.xid = i.xid
< ...even more sql >;
'2021-06-23T08:02:30Z UTC [ LOG: SELECT per_commit_info.day AS day, COUNT(*) AS commits,
";
// 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/