如何找到第三种模式出现在一条线上

时间:2022-03-19 09:26:20

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?

我在VIM中也不知道:如果我只想在一行上找到一个模式的第三次出现,那么regex将是什么?regex是否与使用表格相同?

4 个解决方案

#1


13  

The regex would be:

正则表达式是:

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

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

例如,如果你想把第三个foo改成bar

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

#2


4  

I don't know if it fits your needs, but you can search that way :

我不知道它是否适合你的需要,但你可以这样搜索:

  1. Place your cursor at the beginning line
  2. 将光标放在起始行
  3. Type 3 / pattern Return
  4. 类型3 /模式返回

It place the cursor on the 3rd occurrence of the next matching line (highlighting all occurrences)

它将光标放在下一个匹配行的第三次出现(突出所有的事件)

You can also macro :

你也可以宏:

qa+3nq

qa + 3 nq

then @a to go to the next line 3rd occurence

然后@a转到第三行

#3


3  

For Google users (like me) that search just for: "regex nth occurrence". This will return position of last character of third 'foo' (you need to change {3} to your n and foo to your text):

对于谷歌用户(如我),只搜索:“regex nth事件”。这将返回第三个“foo”字符的最后一个字符的位置(您需要将{3}更改为您的n,将foo更改为您的文本):

length(regexp_replace('lorem ifoopsum foo lor foor1 ipsum foo dolor foo', '((?:.*?foo){3}).*$', '\1'))

This: (?:.*?foo) searches for anything followed by 'foo', then it is repeated 3 times (?:.*?foo){3}, then string from start to (including) 3rd repetition is captured, then rest of string is matched by .*$, then whole string is replaced by captured thing, and length of it is position of last character of 3rd 'foo'.

:(?:. * ? foo)搜索任何“foo”紧随其后,然后是重复3次(?:。* ? foo){ 3 },然后从开始字符串(包括)3日重复捕获,然后匹配字符串的其余部分。* $,然后整个字符串被捕获的东西,和长度是去年的第三“foo”的位置。

#4


1  

Try this:

试试这个:

:Tabularize /^.\{-}\S\s\{2,}

Yes, Tabularize uses Vim's regex, so the example on Eelvex's answer should work.

是的,表格化使用了Vim的regex,因此Eelvex的答案示例应该是可行的。

#1


13  

The regex would be:

正则表达式是:

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

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

例如,如果你想把第三个foo改成bar

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

#2


4  

I don't know if it fits your needs, but you can search that way :

我不知道它是否适合你的需要,但你可以这样搜索:

  1. Place your cursor at the beginning line
  2. 将光标放在起始行
  3. Type 3 / pattern Return
  4. 类型3 /模式返回

It place the cursor on the 3rd occurrence of the next matching line (highlighting all occurrences)

它将光标放在下一个匹配行的第三次出现(突出所有的事件)

You can also macro :

你也可以宏:

qa+3nq

qa + 3 nq

then @a to go to the next line 3rd occurence

然后@a转到第三行

#3


3  

For Google users (like me) that search just for: "regex nth occurrence". This will return position of last character of third 'foo' (you need to change {3} to your n and foo to your text):

对于谷歌用户(如我),只搜索:“regex nth事件”。这将返回第三个“foo”字符的最后一个字符的位置(您需要将{3}更改为您的n,将foo更改为您的文本):

length(regexp_replace('lorem ifoopsum foo lor foor1 ipsum foo dolor foo', '((?:.*?foo){3}).*$', '\1'))

This: (?:.*?foo) searches for anything followed by 'foo', then it is repeated 3 times (?:.*?foo){3}, then string from start to (including) 3rd repetition is captured, then rest of string is matched by .*$, then whole string is replaced by captured thing, and length of it is position of last character of 3rd 'foo'.

:(?:. * ? foo)搜索任何“foo”紧随其后,然后是重复3次(?:。* ? foo){ 3 },然后从开始字符串(包括)3日重复捕获,然后匹配字符串的其余部分。* $,然后整个字符串被捕获的东西,和长度是去年的第三“foo”的位置。

#4


1  

Try this:

试试这个:

:Tabularize /^.\{-}\S\s\{2,}

Yes, Tabularize uses Vim's regex, so the example on Eelvex's answer should work.

是的,表格化使用了Vim的regex,因此Eelvex的答案示例应该是可行的。