删除模式以外的任何内容 [英] Delete anything other than pattern

查看:47
本文介绍了删除模式以外的任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设这是我的文字:

这是我的文字这个
这是我的文字 这是我的文字
我的文字是这样的

我想突出显示除图案之外的所有文本并删除突出显示的文本.
体育课text:这一定是结果.

I would like to highlight all text except pattern and delete the highlighted text.
p.e. text: this must be the result.

文字
文本文本
文字

我找到了如何选择除模式之外的所有文本的代码:
\%(\%(.{-}\)\@!text\zs\)*

I've found the code how to select all text except pattern:
\%(\%(.{-}\)\@!text\zs\)*

但是我不知道如何删除所有突出显示的文本.
这不起作用:
:%s/\%(\%(.{-}\)\@!bell\zs\)*//

however I don't know how to delete all highlighted text.
This doesn't work:
:%s/\%(\%(.{-}\)\@!bell\zs\)*//

有人可以帮我吗?

推荐答案

试试这个:

:%s/\(^\|\(text\)\@<=\).\{-}\($\|text\)\@=//g

说明:

\(^\|\(text\)\@<=\)     # means start of line, or some point preceded by "text"
.\{-}                   # as few characters as possible
\($\|text\)\@=          # without globbing characters, checking that we reached either end of line or occurrence of "text".

另一种方法:

  • 创建一个计算字符串中模式匹配的函数(请参阅:help match() 以帮助您设计)
  • 使用::%s/.*/\=repeat('text', matchcount('text', submatch(0)))
  • Create a function that count matches of a pattern in a string (see :help match() to help you design that)
  • Use: :%s/.*/\=repeat('text', matchcount('text', submatch(0)))

这篇关于删除模式以外的任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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