Replace all non-matching characters between specific columns

I am trying to replace all non-matching characters in one line between specific columns (after searching).

Example :
Search can be everything
In the example below, search = test


Alternative character of mismatched characters: blank space.

I want to replace all non-" test

" characters between columns 10 and 30.
Columns 10 and 30 are denoted by|

before:  djd<aj.testjal.kjetestjaja testlala ratesttsuvtesta !<-a-
                  |                   |   
after:   djd<aj.test       test     testlala ratesttsuvtesta !<-a-

      

How can I figure this out?

+3


source to share


2 answers


Use the following substitution command on this line.

:s/\(test\)\zs\|\%>9v\%<31v./\=submatch(1)!=''?'':' '/g

      



If the column range is specified using visual highlight, run

:'<,'>s/\(test\)\zs\|\%V./\=submatch(1)!=''?'':' '/g

      

+3


source


One method could be to select an appropriate range of columns using visual mode (control + v)

Once selected, find and replace can be done with ( see this question )



 %s/\%Vfoo/bar/g

      

A regex for no test can be found here: Regex to match a string that doesn't contain a word?

+1


source







All Articles