2012-05-16 30 views
3

我玩用vim-紅寶石縮進,並且有一些非常複雜的正則表達式有:暫時解除vim的正則表達式

" 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\):\@!\>'  

用vim文檔的幫助下,我破譯它的意思是:

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

我有些疑惑:

  1. 它真的和''匹配嗎?似乎沒有像那樣工作,但我沒有看到冒號在正則表達式中是什麼特殊字符。
  2. 爲什麼有\zs(比賽開始)和無\ze(比賽結束)?
  3. \%()做了什麼?這只是某種形式的分組嗎?

回答

2
  1. :\@!說,只有當有冒號匹配,如果我正確地讀它。我不熟悉這個匹配的ruby語法,所以這可能不是很正確。有關更多信息,請參見:help /\@!和周圍的主題。

  2. 你可以有一個\zs沒有\ze,它只是意味着匹配的結束是在正則表達式的結尾。相反的情況也是如此。

  3. \%(\)只是創建一個分組,就像\(\)將除了該組不可用作反向引用(將用於:substitute命令)。

1
  1. 可以查看匹配「:」或通過複製正則表達式,並用它來執行與/您所使用的代碼搜索任何其他字符串。使用:set incsearch可以幫助您查看輸入正則表達式時正在匹配的內容。

  2. \zs\ze不影響什麼是匹配的,而是確定哪些匹配文本的部分在功能作爲:s/substitute()。您可以檢查與/'incsearch'選項設置進行搜索 - 你就可以開始在文本字符串,將被突出顯示,然後加入\zs\ze將改變匹配的文本高亮搜索。沒有必要爲「關閉」 \zs\ze,作爲一個可以拋棄僅僅是開始或比賽結束。

  3. 它是分組的形式,不保存在臨時變量的使用與\1\2submatch(),如規定在:h \%()

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