20

Since 6 months I'm using the excellent Vim text-editor. The most interesting thing is Vim's excellent regex support build-in.

I would like to understand regex better in order to ask less questions here :)

I tried Regex coach, Espresso and other regex-help applications but I found out that even if they are working in these applications it often doesn't work in VIM. What kind of regex does Vim use? Are there any help applications in which I can build regex commands for Vim?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Reman
  • 7,931
  • 11
  • 55
  • 97
  • 1
    For anyone stumbling upon this now: if you compiled vim with `+perl` you can always just do `:perldo s/search/replace/g` and use perl syntax. – reem Jan 11 '14 at 21:04

3 Answers3

10

As far as I know vim uses it's own flavor. Here is what the manual says:

Vim's regexes are most similar to Perl's, in terms of what you can do. The difference between them is mostly just notation;

If you want a better (more serious) explanation, look up "Traditional NFA" in Mastering Regular Expressions.

Community
  • 1
  • 1
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 2
    I'm sorry but ViM's regex only vaguely resembles Perl's. For example, in Perl, to match a plus sign use /\+/ in ViM use /+/ In Perl, all non-alphanumerics are escaped with a backslash. In ViM, they're mixed. – shawnhcorey Apr 24 '11 at 13:06
  • 3
    vim has a mixed up regex syntax. I would **really, really** like for a way to turn it into egrep’s or better yet, perl’s. In *nvi* you can use `:set extended` to get egrep style so you don’t have to backwhack parens and plusses and such. – tchrist Apr 24 '11 at 13:22
  • 2
    Vim's regex syntax is not only slightly mixed up (i.e., inconsistent backslashing) but it has several different modes. See `:h magic` for info on the different ways Vim can treat backslashes. – Herbert Sitz Apr 24 '11 at 14:28
  • Vim is hampered a bit by keeping some consistency with traditional Vi syntax, so things can be a bit confusing if coming from a Perl perspective. As Sitz stated, having a look at `:h magic`. – ewh Apr 24 '11 at 21:46
  • 1
    [This script](https://github.com/Raimondi/VimRegEx.vim) can help you with testing of Vim's regex and it can help understanding it too. – Raimondi Apr 25 '11 at 18:02
7

Vim has several regex modes, one of which is very magic that's very similar to traditional regex. Just put \v in the front and you won't have to escape as much.

butterywombat
  • 2,069
  • 7
  • 26
  • 44
2

There is a plugin called eregex.vim which translates from PCRE (Perl-compatible regular expressions) to Vim's regex syntax.

This will make it more likely for regexes from other environments to work, because PCRE have become the de facto industry standard: e.g. Java's java.util.regex, Python's re module, and regexes found in Ruby and JavaScript are all PCREs (each with slight differences).

Evgeni Sergeev
  • 22,495
  • 17
  • 107
  • 124
  • 1
    I would rather use term “Perl-like” and not “PCRE”. PCRE usually means a library (which is used by PHP, zsh with specific USE flag and `rematchpcre` option or `[[ … -pcre-match … ]]`, grep with specific USE flag and `--perl-regexp`/`-P` option). Python and JavaScript do not use it, most likely neither does Java. I bet you will see major differences in supported escape sequences (e.g. `\p{}`, `\x{}`, `\cx`, `\X`, `\N` and many others), `\Q…\E` also are not so common. These are supported by PCRE and Perl, but not by python or javascript (except for `\x{}` and `\cx`). – ZyX Jan 12 '14 at 03:22
  • eregex is exactly what I was looking for. Basically adds the opportunity to use perl-like regexes instead of Vim's own flavour – Eldamir Nov 08 '16 at 08:32