Decryption vim regex

I play with vim-ruby indent, and there are quite complex regular expressions there:

" Regex used for words that, at the start of a line, add a level of indent.
let s:ruby_indent_keywords = '^\s*\zs\<\%(module\|class\|def\|if\|for' .   
      \ '\|while\|until\|else\|elsif\|case\|when\|unless\|begin\|ensure' . 
      \ '\|rescue\):\@!\>' .                                               
      \ '\|\%([=,*/%+-]\|<<\|>>\|:\s\)\s*\zs' .                            
      \    '\<\%(if\|for\|while\|until\|case\|unless\|begin\):\@!\>'     

Using the vim documentation, I decrypted it to mean:

start-of-line <any number of spaces> <start matching> <beginning of a word> /atom
<one of provided keywords> <colon character> <nothing> <end of word> ...

I have some doubts:

  • Does this really match ':'? This doesn't seem to be the case, but I see nothing about the fact that the colon is a special character in regular expressions.
  • Why does it exist \zs(start of a match) and not \ze(end of a match)?
  • what is he doing \%()? Is this just some form of grouping?
+3
source share
2 answers
  • :\@! , , . ruby, , . . :help /\@! .

  • \zs \ze, , . .

  • \%(\) , \(\), , (, :substitute).

+2
  • ':' , / , . :set incsearch , .

  • \zs \ze , , :s/substitute(). , / 'incsearch' - , , \zs \ze . "" \zs \ze, .

  • , \1, \2 submatch(), :h \%():

    \%(\) A pattern enclosed by escaped parentheses. Just like \(\), but without counting it as a sub-expression. This allows using more groups and it a little bit faster.

+1

All Articles