Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
534 views
in Technique[技术] by (71.8m points)

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

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

p.e.

<ScrollWheelDown>    move window     three lines     down  
<S-ScrollWheelDown>     move window    one page   down
<ScrollWheelUp>        move window      three lines up
<S-ScrollWheelUp>    move window   one page      up

I use Tabular plugin to align tables but I could not find a way how to find only the first occurrence of multiple spaces and do an align only there.

I don't know it either in VIM: What will be the regex if I only want to find the 3rd occurrence of a pattern on a line? Is the regex the same as using Tabular?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...