惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tailwind CSS Blog
The GitHub Blog
The GitHub Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
Microsoft Security Blog
Microsoft Security Blog
Stack Overflow Blog
Stack Overflow Blog
量子位
Martin Fowler
Martin Fowler
月光博客
月光博客
P
Proofpoint News Feed
博客园_首页
Y
Y Combinator Blog
I
InfoQ
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
H
Help Net Security
U
Unit 42
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
An Introduction to Lookaround Regular Expression in Neovi...
2018-10-18 · via jdhao's digital space

Today I want to use some regular expressions for searching and replacing in my files. But I found that Nvim/Vim regular expression engine has its own flavor, which is different from the regex engine used by Sublime Text1. I ended up learning some of the basics of Nvim-style regex. In this post, I want to share about how to use lookaround2 in Nvim.

First, in Nvim, the syntax for look around is:

  • \@=: positive lookahead
  • \@!: negative lookahead
  • \@<=: positive lookbehind
  • \@<!: negative lookbehind

But how to use look around in Nvim? Let is take an example to explain this. Suppose we have text as follows:

To match foo which is followed by bar (bar not part of the match), use the following regex:

In Nvim, ( and ) matches literally, which is different from PCRE. In order to use lookaround in Nvim, we have to include the lookaround pattern in a group, hence the escaped parentheses \(\) around bar.

So the first foo in the above text matches:

foobar foo barfoo
^
one match here

Similarly, we have the following 4 patterns:

  1. Match foo which is not followed by bar: /foo\(bar\)\@!
# now we have two matches
foobar foo barfoo
       ^      ^
       two matches here
  1. Match foo which is preceded by bar: /\(bar\)\@<=foo
foobar foo barfoo
              ^
              one match here
  1. Match foo which is not preceded by bar: /\(bar\)\@<!foo
foobar foo barfoo
^      ^
two matches here
  1. Match foo which is neither followed nor preceded by bar: /\(bar\)\@<!foo\(bar\)\@!
foobar foo barfoo
       ^
       one match here

Combine magic mode and look around#

Several months after I first wrote this article, I learned that Nvim also has a very magic mode (see :h /magic for more info) which behaves more like PCRE. The very magic mode is activate by preceding the search pattern with \v. In the very magic mode, you have to remove the \ character from the look around pattern. So in the very magic mode, the following regex should be used instead:

  • positive look ahead: @=
  • negative look ahead: @!
  • positive look behind: @<=
  • negative look behind: @<!

For example, to match foo which is followed by bar, you should use:

\ze and \zs for positive lookaround#

We may also use \zs and ze for positive lookbehind and lookahead. \zs means that the actual match starts from here and \ze means that the actual match ends here. Take the above foobar text as an example:

  • Match foo preceded by bar: \bar\zsfoo
  • Match foo followed by bar: foo\zebar

In the above two match, bar is not part of the match. You can see that\ze and \zs simplify the pattern for positive lookaround.

A last example#

Let’s take another example (adapted from this post):

rs11223-A        -A
rs23300-G        -TTA
rs9733-T          -G
rs11900000-GT    -TTG

Suppose we want to find - in the first part of each line, how do we write the regex pattern?

Using positive lookbehind#

With positive lookbehind, the pattern can be (definitely not the only one):

In the above regex pattern, \+ means to match previous pattern 1 or more time, which is a bit different from what we are accustomed to in Sublime Text.

Using \zs#

With \zs, we can simplify the match pattern:

Ok, that is all. Hope you can finally understand how to correctly use lookaround regex in Nvim.

References#