Here is my data:

{ team: 'NYY', result: 'L', rs: 1,6 },
{ team: 'NYY', result: 'W', rs: 4,3 },
{ team: 'NYY', result: 'L', rs: 3,6 },
{ team: 'NYY', result: 'L', rs: 5,6 },
{ team: 'NYY', result: 'L', rs: 4,8 },
{ team: 'NYY', result: 'W', rs: 14,4 },
{ team: 'NYY', result: 'W', rs: 6,5 },
{ team: 'NYY', result: 'L', rs: 3,4 },
{ team: 'NYY', result: 'L', rs: 5,7 },
{ team: 'NYY', result: 'W', rs: 5,4 },
{ team: 'NYY', result: 'W', rs: 9,0 },
{ team: 'NYY', result: 'W', rs: 5,3 },

I would like to replace the last comma in each line so the line looks like this:

{ team: 'NYY', result: 'L', rs: 1, ra: 6 },

Can I use :%s to accomplish this?


Peter's answer achieves the result that you want, but not necessarily the question that you asked.

This regular expression will match the Nth occurrence of your PATTERN:


To unpack that a little bit, it matches everything non-greedily (that's what the {-} is) though each PATTERN N times (that's what the {N} is)

The \zs marks the start of what you want to replace. In the example above, you would end up replacing the comma with whatever your replacement text is. You could put the \zs after the pattern to add your replacement after the comma.

To give a more concrete example with your data,

%s/\(.\{-},\zs\)\{3}/ ra: /

would add in the text after the third comma.

That being said, this doesn't really seem to be the best approach for this particular problem. Here, you could insert your "ra" in between any comma followed by a digit:

%s/,\zs\ze\d/ ra: /g

Or use a macro to find the Nth occurrence and insert:

3f,a ra:

Or use Tabular to align your commas, and then do a block select (ctrl+v) to insert your text:

Tab /,


  • Awesome answers, the both of you. – Jeff Diederiks Apr 20 '16 at 15:20

Using \ze makes this pretty easy. Just match the end of the line so digit followed by ending curly bracket.

%s/\ze\d\+ }/ ra: /

\ze sets the end of the matched area. For more help see :h /\zs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.