Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Vim have a character limit for regexes?

Tags:

vim

I just tried (naively) to put together a macro for upper-casing an arbitrary set of SQL reserved words.

:nnoremap <leader>c :s/\(\<use\>\)\|\(\<create\>\)\|\(\<select\>\)\|\(\<update\>\)\|\(\<delete\>\)\|\(\<not\>\)\|\(\<null\>\)\|\(\<unique\>\)\|\(\<constraint\>\)\|\(\<references\>\)\|\(\<join\>\)\|\(\<on\>\)\|\(\<inner\>\)\|\(\<outer\>\)\|\(\<left\>\)\|\(\<group\>\)\|\(\<order\>\)\|\(\<having\>\)\|\(\<by\>\)/\U&/g<CR>

The macro is written to my .vimrc, which loads up fine. But when I run the macro Vim spits out some errors:

E872: (NFA regexp) Too many '('
E51: Too many \(
E476: Invalid command

I've been searching around but found nothing to indicate that there are limits on regexes. My best read on the errors is that I've failed to properly escape something, but I can't find it.

Have I exceeded some limit on regexes here?

like image 925
Nate Kennedy Avatar asked Apr 26 '16 15:04

Nate Kennedy


2 Answers

You can only have capture patterns \1..\9. If you need more groupings, but don't need to capture them all, you can use non-capturing groups with the \%(pattern\) syntax:

/abc\%(def\)ghi/
like image 109
Droj Avatar answered Sep 29 '22 19:09

Droj


It is not about the limit of characters in regex, it is about the limit of number of groups in your regex.

Vim can handle max 10 regex groups, (\0....\9), I didn't count your codes, but you should have more than 10 groups in your regex.

The constant (10) was defined in regexp.h

https://github.com/vim/vim/blob/0b9e4d1224522791c0dbbd45742cbd688be823f3/src/regexp.h#L22

And it was used to validate user's regex, like here :

https://github.com/vim/vim/blob/af98a49dd0ef1661b4998f118151fddbf6e4df75/src/regexp.c#L1539

like image 29
Kent Avatar answered Sep 29 '22 20:09

Kent