$re = '/\[\[(?:[^][]*|\[(?!\[)|\](?!\]))*+\]\]\K|\[\[ ?| ?\]\]/';
$str = '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 ]]]]';
$subst = "";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$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 PHP, please visit: http://php.net/manual/en/ref.pcre.php