regexreplacevim

vim substitution : # for a single $ and nothing to do if $$


I would like to do the following substitution : replace $ of words located between 2 $ by the symbol # and do nothing if these words are between 2 $$.

For example, in this text :

Currently, we are able to make cross correlations (understand "combine" to have better constraints on cosmological parameters) between weak lensing (WL) and photometric Galaxy clustering (GCph). When I say combine, as in the case where I have 2 sets of different measures ($\tau_1, \sigma_1$) and ($\tau_2, \sigma_2$), well, if I consider the Gaussian errors, it is shown quite easily (by Maximum Likelihood Estimation) that the estimator $\sigma_{\hat {\tau}}$ the most representative matches the relation:
    
    
$$\dfrac{1}{\sigma_{\hat{\tau}}^{2}}=\dfrac{1}{\sigma_1^2}+\dfrac{1}{\sigma_2^2}$$

The substitution would give :

Currently, we are able to make cross correlations (understand "combine" to have better constraints on cosmological parameters) between weak lensing (WL) and photometric Galaxy clustering (GCph). When I say combine, as in the case where I have 2 sets of different measures (#\tau_1, \sigma_1#) and (#\tau_2, \sigma_2#), well, if I consider the Gaussian errors, it is shown quite easily (by Maximum Likelihood Estimation) that the estimator #\sigma_{\hat {\tau}}# the most representative matches the relation:


$$\dfrac{1}{\sigma_{\hat{\tau}}^{2}}=\dfrac{1}{\sigma_1^2}+\dfrac{1}{\sigma_2^2}$$

I tried to perform this with the following command under vim :

%s/\$[^\$]\(.*\)\$[^\$]/#\1#/g

or

%s/\$[^\$]\(.\{-}\)\$[^\$]/#\1#/g

But this doesn't work. I can't see my error.


Solution

  • You may use this regex replacement in vim using negative lookahead and negative lookbehind:

    Search:

    :%s/\v\$@<!\$([^$]+)\$\$@!/#\1#/g
    

    Replacement:

    #\1#
    

    Explanation: