From my point of view the solution of @Mattb is probably the better.
But for the sake of completion here is an attempt to provide an answer purely based on regular expression.
To match GD[many]
you can use:
/\vGD\[[^\[\]]*\]
To match GD[many]
or GD[foo[bar]many[foo]bar]
\vGD\[[^\[\]]*(\[[^\[\]]*\][^\[\]]*)*\]
To match GD[many]
or GD[foo[bar]many[foo]bar]
or GD[foo[bar[many]foo]bar]
\vGD\[[^\[\]]*(\[[^\[\]]*\][^\[\]]*)*(\[[^\[\]]*(\[[^\[\]]*\][^\[\]]*)*\][^\[\]]*(\[[^\[\]]*\][^\[\]]*)*)*\]
Here is a recursive function that return the expression that match the 'inside' depending of the number of bracket that you want to allow.
function! SearchExpression(n)
if a:n==0
return '[^\[\]]*'
else
let expression = SearchExpression(a:n - 1)
return expression . '(\[' . expression . '\]' . expression . ')*'
endif
endfunction
You can use it in search mode:
:exe '/\vGD\[' . SearchExpression(2) . '\]'
Or in substitute mode:
:exe '/\vGD\[' . SearchExpression(1) . '\]/someStuff/'
:exe '/\vGD\[' . SearchExpression(2) . '\]/someOtherStuff/'
:help searchpair
. I'd think you'd need to write a function using this that replaces stuff inside the outermost brackets (if that's worth the time for you).ci]
normal mode command with the cursor positioned in the right place to target the pairs of brackets that you care about. Question: do the brackets that you care about always haveGD
right before them?GD[
occurrences? With a macro, you would only need to do the work once, and then run the macro on all lines with:%norm @q
(assuming you recorded into theq
register). I have a feeling that the:%s
way might not have the power to 'know' which brackets are paired (I could be wrong).GD[
. Your suggestion is very inspiring. I didn't know of the existence ofnorm
. Using this andci[
should solve my problem. I have written a python script to tackle it, but could try it next time when having similar problems. Thanks!