Search code examples
regexvim

regex to capture the second space in a line?


I've got the following text:

get close to,be next to,follow in sequence or along designated route/ direction

How would I match the space after the "e" in close and then replace it with a tab?

Although this may be easy to all of you, I've spent 4 hours trying to do this but haven't been successful.

The general rule would be "match only the space after the second word". I've got over 2000 unique lines which is why I need a regex.

Thank you!!


Solution

  • In vi editor you can use this search/replacement:

    :s/^\([[:blank:]]*\w\+[[:blank:]]\+\w\+\)[[:blank:]]\+/\1\t/
    

    Or shorter (thanks to @PeterRincker):

    :%s/\v(\w+\zs\s+){2}/\t