I'm not sure of the right language here, but I need to modify all occurrences of a pattern (specifically &) that occur inside a pair of delimiters while leaving unmodified the pattern if it is 'outside all open delimiters'. With apologies, here's a complicated real world example (I'm concerned that an answer to a simplified example might exploit special features of the example that won't hold in general.)
In the following line, the first & is outside all delimiters, but the second one is located to the right of a { and to the left of the matching }. I need to modify the second one but not the first.
\begin{array}{l l}E\left( {\tau |k} \right) & = E\left[ {\left. {\left( {\frac{1}{{n - 2d}}\sum\nolimits_{i = 1}^n {{Z_i}} } \right)\tau } \right|k} \right] = \frac{1}{{n - 2d}}\sum\nolimits_{i & = 1}^n {E\left( {{Z_i}\tau |k} \right)} \\
Here's an even nastier example.
\left| {E\left( {X|A} \right)} \right| & = \left| {E\left( {{q_2}|{q_1} < {q_2} < {q_3} < {q_4}} \right)} \right| & = \frac{{\left| {E\left( {{q_2}{I_{{q_1} & < {q_2} & < {q_3} & < {q_4}}}} \right)} \right|}}{{P\left( A \right)}}
Again, the first and second &'s are outside of any matching delimiters, but the third, fourth and fifth are enclosed in matching delimiters, and need to be modified.
Thanks very much for any advice, and apologies for the persnickety nature of this horrible problem.