cabe56
cabe56

Reputation: 404

Why would a regex work in Sublime and not in vim?

Tried searching for regex found in this answer:

(,)(?=(?:[^']|'[^']*')*$)

I tried doing a search in Sublime and it worked out (around 700 results). When trying to replace the results it runs out of memory. Tried /(,)(?=(?:[^']|'[^']*')*$) in vim for searching first but it does not find any instances of the pattern. Also tried escaping all the ( and ) with \ in the regex.

Upvotes: 1

Views: 161

Answers (4)

romainl
romainl

Reputation: 196546

Vim uses its own regular expression engine and syntax (which predates PCRE, by the way) so porting a regex from perl or some other editor will most likely need some work.

The many differences are too numerous to list in detail here but :help pattern and :help perl-patterns will help.

Anyway, this quick and dirty rewrite of your regular expression seems to work on the sample given in the linked question:

/\v(,)(\@=([^']|'[^']*')*$)

See :help \@= and :help \v.

Upvotes: 3

shock_one
shock_one

Reputation: 5925

Unfortunately Vim uses a different engine, and "normal" regular expressions won't work. The regex you've mentioned isn't perfect: it doesn't skip escaped quotes, but, as I understand, it's good enough for you. Try this one, and if it doesn't match something, please send me that piece.

\v^([^']|'[^']*')*\zs,

A little explanation:

\v enables very magic search to avoid complex escaping rules

([^']|'[^']*') matches all symbols but quote and a pair of qoutes

\zs indicates the beginning of selection; you can think of it as of a replacement for lookbehind.

Upvotes: 1

spencer7593
spencer7593

Reputation: 108400

One possible explanation is that the regular expression engine used in Sublime is different than the engine used in vim.

Not all regex engines are created equal; they don't all support the same features. (For example, a "negative lookahead" feature can be very powerful, but not all engines support it. And the syntax for some features differs betwen engines.)

A brief comparison of regular expression engines is available here:

http://en.wikipedia.org/wiki/Comparison_of_regular_expression_engines

Upvotes: 1

miindlek
miindlek

Reputation: 3553

You have to escape the |, otherwise it doesn't work under vim. You should also escape the round brackets, unless you are searching for the '(' or ')' characters.

More information on regex usage in vim can be found on vimregex.com.

Upvotes: 0

Related Questions