Substitution template for return quotes in vim

I have text with some lines that contains properly escaped double quotes in the lines, and some as shown below:

bla1 "aaa"bbb"ccc" bla1
bla2 "aaa\"bbb\"ccc" bla2

Results after substitution should be

bla1 "aaa\"bbb\"ccc" bla1
bla2 "aaa\"bbb\"ccc" bla2

but not:

bla1 "aaa\"bbb\"ccc" bla1
bla2 "aaa\\"bbb\\"ccc" bla2

In other words, it should avoid double quotes in strings where they are not escaped and do not touch lines that are already properly escaped

So far I got a second result with this

%s:\(\s".\+\)\(".\+\)\(".\+"\s\):\1\\\2\\\3:g

Then I tried a negative lookbehind to tell the engine not to match if there is a backslash before the quotes

(?<!\) which in vim should be something like @<!\

%s:\(\s".\+\)\@<!\\\(".\+\)@<!\\\(".\+"\s\):\1\\\2\\\3:g

But I think I lost a little.

Note:
There is only one line in the line. The line is enclosed in double quotes and may contain double quotes inside - only inside this should be escaped

+3
4

, 1 , , . ( )

:%s/"\zs.*\ze"/\=substitute(submatch(0), '\\\@<!"', '\\"', 'g')

:

  • :%s/"\zs.*\ze" . .*. \zs , \ze .
  • , \= . , .

    substitute(submatch(0), '\\\@<!"', '\\"', 'g')
    

    submatch(0) - . , (\\\@<!") \".

:h sub-replace-expression, :h /\zs :h /\ze


:

bla1     "aaa"bbb"ccc"      bla1
bla2     "aaa\"bbb\"ccc"    bla2
bla\bla3 "aaa"bbb"ccc"      bla3 
blabla4  "aaa"bbb" "BBB"ccc" bla4       
bla\bla5 "aaa"bbb" "BBB"ccc" bla5
bla\bla5 "aaa"bbb""BBB"ccc" bla5

:

bla1     "aaa\"bbb\"ccc"      bla1
bla2     "aaa\"bbb\"ccc"    bla2
bla\bla3 "aaa\"bbb\"ccc"      bla3 
blabla4  "aaa\"bbb\" \"BBB\"ccc" bla4       
bla\bla5 "aaa\"bbb\" \"BBB\"ccc" bla5
bla\bla5 "aaa\"bbb\"\"BBB\"ccc" bla5
+2

. , :

:v/\\"/s:\(\s".\+\)\(".\+\)\(".\+"\s\):\1\\\2\\\3:g
+1
:%s/\([^ \\]\)"\([^ ]\)/\1\\"\2/g

This finds quotation marks not preceded by a slash or space, and not followed by a space.

0
source

This might work:

%s/\([^\\]\)\("\)/\1\\\2/g
0
source

All Articles