On Tue, Mar 31, 2009 at 10:13 AM, Andreas Bernauer <[email protected]>wrote:
>
> ivan budiselic wrote:
> >>> 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")
> >>
> >> ...because \{-} matches also 0 times; what you wanted is \{-1,}:
> >>
> >> let line = substitute('123 a "a" a', '\([^"]\{-1,}\)a', '\1b', 'g')
> >>
> >
> > Ok, this worked pretty well, except that it doesn't work for cases when
> > there's an 'a' at the start of the line (which I might be able to fix),
> and
>
> Allow matching 'start-of-line' (^) in front of 'a', too; see below.
>
> > the other problem which I wasn't specific enough about, and that is that
> I'd
> > like the change to happen to 'a's outside of strings, so for example
> > "xxxaxxx" shouldn't match. I guess this changes things quite a bit, sorry
> > for not being clear enough.
>
> So, would you like further help? If yes, could you provide an example for
> what
> you want to achieve?
>
> s/\(^\|\s\)\zsa\ze\s\?/b/
> works for
> a 123 a a "xxxxaxxx" a
> ==>
> b 123 b b "xxxxaxxx" b
>
Ok, so a complete example would be this:
a 123 a "xy a xax xa a" xa ax xax "a" " a " " xa ax xax " a
should result in
b 123 b "xy a xax xa a" xb bx xbx "a" " a " " xa ax axa " b
In words, all occurrences of 'a' outside of double quotes change to b, and
all occurrences of 'a' inside double qoutes stay uncanged.
The line you suggested doesn't handle the quotes. What I tried now is:
let line = substitute(line, '^\%([^"]\|.\{-}".\{-}".\{-}\)\{-}\zsa\ze', 'b',
'g')
Now, the problem I'm having is that I'm obviously missunderstanding the 'g'
flag. This line does what I want, but only does the substitution for the
first 'a' (so I can solve the problem by running it multiple times, until a
match is possible, which is what I thought the 'g' flag is supposed to do).
The idea behind the expressions is that the match should happen if there is
an even (possibly zero) number of double-quote characters to the left of an
'a' (and, again, it seems to be working, but only on the first match).
I'm sorry if I'm bothering you with this problem, your help is greatly
appreciated.
Ivan
--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---