// 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"\[\[(?:[^][]*|\[(?!\[)|\](?!\]))*+\]\]\K|\[\[ ?| ?\]\]").unwrap();
let string = "Locate instances of nested double square brackets and remove the outer double square brackets
I'm using TextMate (but happy to use any suitable search and replace program) to query a set of files (these files are my notes in Logseq if its relevant).
I'm looking to find and replace instances of nested double square brackets and remove the outer double square brackets
eg 1 - Normal nesting
[[ any text or no text [[ any text ]] any text or no text]]
eg 2 - Compound nesting
[[ any text or no text [[ any text ]] [[ any text ]] any text or not text ]]
eg 3 - multi-level nesting
[[ any text or no text [[ any text or no text [[ any text or no text ]] any text or no text]] any text or no text ]]
Note: keep in mind that the double square brackets could be touching. So example 1 could also manifest as
[[ any text or no text [[ any text ]]]]";
let substitution = "";
// result will be a String with the substituted value
let result = regex.replace_all(string, substitution);
println!("{}", result);
}
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/