如何在一行中找到第 3 次出现的模式 [英] How to find the 3rd occurrence of a pattern on a line

查看:16
本文介绍了如何在一行中找到第 3 次出现的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我不得不在一行的前多个空格处对齐表格.

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

我使用 Tabular 插件来对齐表格,但我找不到如何仅找到多个空格的第一次出现并仅在那里对齐的方法.

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.

我在 VIM 中也不知道:如果我只想在一行中找到第 3 次出现的模式,那么正则表达式是什么?正则表达式和使用 Tabular 一样吗?

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?

推荐答案

正则表达式为:

/(.{-}zsPATTERN){3}

例如,如果您想在以下行中将第 3 个 'foo' 更改为 'bar':

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

运行:

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

获得:

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

这篇关于如何在一行中找到第 3 次出现的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆