How to find the 3rd occurrence of a pattern on a line

后端 未结 4 2205
囚心锁ツ
囚心锁ツ 2020-12-05 15:10

Today I had to align a table at only the first multiple spaces on a line.

p.e.

    move window     three lines     down  
<         


        
4条回答
  •  离开以前
    2020-12-05 15:33

    The regex would be:

    /\(.\{-}\zsPATTERN\)\{3}
    

    So if, for example, you want to change the 3rd 'foo' to 'bar' on the following line:

    lorem ifoopsum foo lor foor ipsum foo dolor foo
           ^1      ^2      ^3         ^4        ^5
    

    run:

    s/\(.\{-}\zsfoo\)\{3}/bar/
    

    to get:

    lorem ifoopsum foo lor barr ipsum foo dolor foo
           ^1      ^2      ^3=bar     ^4        ^5
    

提交回复
热议问题