Why does VIM have its own regex syntax?

RegexVim

Regex Problem Overview


Why do you have to make your regex "very magic" so that you don't have to escape your capture quotes? And if you set your environment to very magic, you are non-standard and may have compliance issues. I am just wondering why vim uses a different regex syntax than say, perl?

Regex Solutions


Solution 1 - Regex

Most vi (and therefore vim) features were derived from ed. vi and ed both predate perl by at least a decade or two. A better question might be "why doesn't Perl use the same regex syntax as vi?".

Of course, one could also argue that the kinds of regular expressions that one would wish to write inside a text editor to perform common tasks are probably rather different to those you might wish to write inside a programming language.

Solution 2 - Regex

There is a plugin called eregex.vim which translates from PCRE to Vim's syntax. It takes over a thousand lines of vim to achieve that translation!

Solution 3 - Regex

In addition to the mentioned reasons vim has some cards in the sleeve when it comes to regex, for example, to match positive look behind we do:

:%s/^foo \zsbar/moo/g

The above command will substitute "bar" for "moo" in the lines started with "foo".

the \zs makes easier to set a positive look-behind and \ze makes easier to make a positive look-ahead.

We also have a way to match only in the "visual area" -> %V

:'<,'>s/\%Vthis/that/g

Although using the global flag "g" the substitution is restricted to the visual are due \%V

To read an awesome article about how amazing vim regexes can be, follow this link: https://bitbucket.org/snippets/sergio/7nzqxx

We can also use some "submatch" tricks on vim substitution https://dev.to/voyeg3r/vim-math-on-vim-substitution-4obo

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser210757View Question on Stackoverflow
Solution 1 - RegexGianView Answer on Stackoverflow
Solution 2 - RegexEvgeni SergeevView Answer on Stackoverflow
Solution 3 - RegexSergioAraujoView Answer on Stackoverflow