On 06/07/09 09:44, Jürgen Krämer wrote:
>
>
> Hi,
>
> Jeri Raye wrote:
>>
>> Can you use the position of a word in a search and replace as an argument
>>
>> For example
>>
>> if the line has the word INPUT in it, then the last word should have
>> () around it
>>
>> So:
>>
>> start:     INPUT s1 , s2
>>              INPUT s3 , s4
>>
>> must become
>>
>> start:     INPUT s1 , (s2)
>>              INPUT s3 , (s4)
>
>    g/\<INPUT\>/s/\(.*\)\(\<\w\+\>\)/\1(\2)/
>
> or
>
>    g/\<INPUT\>/s/.*\zs\(\<\w\+\>\)/(\1)/
>
> do this. The :global-command restricts the following substitution to
> lines containing the word INPUT. The :substitute-commands first try to
> match as many characters as possible in a line, but then they backtrack
> to be able to match the right-most single word. The first command then
> replaces everything that was matched with the part before the word and
> the word itself in parentheses. The second command uses \zs to reset the
> start of the match and replaces only the word with itself in parentheses.
>
> Regards,
> Jürgen
>

The OP's request sounds like school homework, so I'd recommend to read
        :help :s
        :help :g
        :help pattern-overview
and what comes before and after them.

Just this time, to show that Vim solutions are rarely unique, I'll 
propose another solution (untested):

        :g/\<INPUT\>/s/\w\+\ze\W*$/(\0)

This ought to replace, on lines containing the word INPUT, as many 
word-characters as possible by the same thing in parentheses, but only 
if followed by nothing else than zero or more non-word characters till 
the end of the line. (\zs and \ze mark the start and end of what should 
be replaced -- very handy).

Note that both Jürgen's solution and mine will replace a line consisting 
only of the word INPUT by (INPUT) -- if you want not to replace in that 
case, well, it's doable: "the solution is left as an exercise to the 
reader".


Best regards,
Tony.
-- 
XIIdigitation, n.:
        The practice of trying to determine the year a movie was made
by deciphering the Roman numerals at the end of the credits.
                -- Rich Hall, "Sniglets"

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to