ivan budiselic wrote:
> Hi,
>
> I'll post a simplified version of my problem. Basically, I want to
> substitute every occurrence of 'a' (the letter a, without the quotes) with
> 'b', but only if 'a' is not inside a string, by which I mean it is not
> enclosed in double quotes.
>
> So, for the line:
>
> 1234 a a "a" a
>
> I'd like to get:
>
> 1234 b b "a" b
>
> The question is, is this possible and how.
>
> What I've tried is
> let line = substitute(line, '\([^\"]\{-}\)a', "\\1b", "g")
You can use the \zs and \ze markers, which mark the beginning and end of the
match to be substituted.
:s/[^"]\zsa\ze[^"]\?/b/
An alternative is to use several substitutions: \
:s/"a"/xxx/
:s/a/b/
:s/xxx/"a"
Provided, xxx does not appear in your text.
> There are a few obvious problems with this. First off, I'm not sure whether
> the \ before the " is necessary (Vim regex syntax is unfortunately sort of
> idiosyncratic), but in any case, neither version works. The output I get for
> the previous line is:
> 1234 b b "b" b
...because \{-} matches also 0 times; what you wanted is \{-1,}:
let line = substitute('123 a "a" a', '\([^"]\{-1,}\)a', '\1b', 'g')
(which does what you want; btw, note that the double quote '"' is not quoted).
See
:help /\{-
:help \zs
> Also, the above code in no way addresses the part "behind" the strings.
In your example it does, though?
Cheers,
--
Andreas.
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---