Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why won't this regex work in SublimeText when it does in Vim?

I have this regex

^\([^\t]*\)\t\([^\t]*\)\t\([^\t]*\)$

which is supposed to match

  1. beginning of the line
  2. a capture of all letter until a Tab
  3. a capture of all letter until a Tab
  4. a capture of all letter until a Tab
  5. the EOL

In Vim, this works fine: correct regex capture

But in Sublime it will not match. Why?

like image 206
New Alexandria Avatar asked Sep 01 '18 19:09

New Alexandria


1 Answers

Vim regex is rather specific and differs from the PCRE regex engine expression syntax that Sublime Text 3 uses.

In Sublime Text 3, you can write the pattern you used in Vim as

^([^\t\r\n]*)\t([^\t\r\n]*)\t([^\t\r\n]*)$

See the regex demo

In short, (...) should be used to form a capturing group, and you need to add \r\n to disallow a negated character class to match across lines (in Vim, [^.]* won't match a line break, but it will in Sublime Text 3).

Note that (...) (and not \(...\)) can also be used as a capturing group in Vim, but you need to use very magic mode to use that syntax.

like image 68
Wiktor Stribiżew Avatar answered Nov 15 '22 07:11

Wiktor Stribiżew