English 中文(简体)
振动中的 亚插件替代树枝
原标题:Submatches in vim substitute branches
  • 时间:2012-05-23 21:01:28
  •  标签:
  • regex
  • vim

我要使用多个分支搜索, 从匹配分支中取下子匹配, 用于替换 。

对于这一具体问题,我想抓住完全由=或-(较小子集)中一个字符构成的任何线条,其中至少有3个字符,每个字符的空格条件不同。

因此,我用以下表达方式来与它们相对应:

^	(=){3,}\_$|[ ]{5,}^(-){3,}\_$

我接下来想做的是用潜水艇来替代

:.s/^	(=){3,}\_$|[ ]{5,}^(-){3,}\_$/apple1banana/e

但似乎只有第一个分支(和第二个分支)相匹配,而不是第二个分支才管用。 我怎样才能让这个分支工作? 我在找一个解决方案, 它可以让我使用20个分支, 并且仍然能够从匹配分支获得分母。

最佳回答

您可以使用零边图案和回溯引用来做到这一点 :

(\%(^	)@<=={3}|\%([ ]{5,}^)@<=-{3})\_$

零维模式允许您将一个模式从匹配某些字符转换为匹配位置。

zs 是一个特例, 它可以简化以 lt; /code> 开始到 ...zs 的任何模式。

另一种办法只是使用替换中的所有背引号 -- -- 来自不匹配分支的背引号将扩大为空字符串:

:.s/^	(=){3,}\_$|[ ]{5,}^(-){3,}\_$/apple12banana/e
问题回答

从我看来,你不需要团体。这似乎与你的命令相同(我没有样本可以测试):

:.s/^	zs=ze={2,}$|^[ ]{5,}zs-ze-{2,}$/apple&banana/e

它将把全部的 [- =] 放在替换中:

:.s/^	zs={3,}$|^[ ]{5,}zs-{3,}$/apple&banana/e

如果您可以分享您需要编辑的文本样本和预期结果, 这会更容易测试我写的东西是否有效 。





相关问题
Autoupdate VIM Plugins?

Is it possible to update vim plugins automatically?

how to unindent in vim without leaving edit mode?

I m writing a lot of python code recently, and i used the tab-to-space mode in vim. I was just wondering how would i unindent in vim without leaving edit mode for example after i finished if...: block....

Scrolling inside Vim in Mac s Terminal

I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...

Vim - Deleting XML Comments

How do I delete comments in XML? If the opening and the closing comment tags are on the same line, I use :g/^<!--.*-->$/d to delete the comment. How to delete the comments that are spread ...

Limiting a match in vim to certain filetypes?

I have the following in my .vimrc to highlight lines longer than 80 chars: highlight OverLength ctermbg=red ctermfg=white guibg=#592929 match OverLength /\%81v.*/ This works quite well. However, the ...

Profiling Vim startup time

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of ...

热门标签