Search code examples
regexvim

Remove symbol after n'th occurrence in Vim


I want to remove semicolons after the 7th occurrence, on multiple rows that may look like this:

foo;10.10.10.10;/24;;NA;;;foo; bar; "foobar"

Meaning that the result should be like this:

foo;10.10.10.10;/24;;NA;;;foo bar "foobar"

What I have been able to do so far is to segment the parts into capture groups:

:%s/(.{-};.{-};.{-};.{-};.{-};.{-};.{-};)(.*)

My problem is that I don't know how to delete characters within a capture group - how do I go about this?


Solution

  • One way to do it:

    :%s/\v^([^;]*;){7}\zs.*/\=substitute(submatch(0), ';', '', 'g')/