2012-03-27 32 views
1

我試圖在包含代碼和自定義評論標記的文件上運行正則表達式(Ruby)。我想查找(/*+ ... +*/),單行或多行之間的所有文本。使用正則表達式在多個開始標記和結束標記之間查找內容

考慮:

/*+ 
    # Testing Documentation # 
    ## Another Documentation Line ## 
    This is also picked up 
+*/ 
Some code here that is ignored 
/*+ # More documentation # +*/ 

我希望每個組文本的開放和/*+ ... +*/

我已經試過以下REG前這對於單行例子的偉大工程閉合之間匹配。但是,如果我啓用多行選項,它會選取第一個匹配和最後一個匹配之間的所有內容,而不是匹配兩個或多個組。

/(?<=\/\*\+)(.*)(?=\+\*\/)/ 

感謝

回答

3

在中間非貪婪((.*?))中進行匹配。這裏有一個永久鏈接Rubular:

http://rubular.com/r/0SuNNJ3vy6

我所用的表達/(\/\*\+)(.*?)(\+\*\/)/m,微調,您認爲合適。

text.scan(/(\/\*\+)(.*?)(\+\*\/)/m).map(&:join) 
#=> ["/*+\n # Testing Documentation #\n ## Another Documentation Line ##\n This is also picked up\n+*/", "/*+ # More documentation # +*/"] 
+0

非常好。謝謝。 – 2012-03-27 14:52:06

0

也許這會幫助你的。這在JS中有效:

/(?:\/\*\+)([\w\W]*?)(?=\+\*\/)/igm 
相關問題