Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 992
Reman Avatar asked Mar 24 '11 17:03

Reman


People also ask

How do you find the nth occurrence of a character in a string?

1) Select Lookup from the drop-down list of Formula Type section; 2) Choose Find where the character appear Nth in a string in Choose a formula section; 3) Select the cell which contains the string you use, then type the specified character and nth occurrence in to the textboxes in the Arguments input section.

How do you find the nth occurrence of a character in a string in python?

You can find the nth occurrence of a substring in a string by splitting at the substring with max n+1 splits. If the resulting list has a size greater than n+1, it means that the substring occurs more than n times.

Where is second occurrence of a character in a string PHP?

Simplest solution for this specific case is to use the offset parameter: $pos = strpos($info, '-', strpos($info, '-') + 1);


1 Answers

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
like image 183
Eelvex Avatar answered Oct 03 '22 01:10

Eelvex