2012-03-27 117 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 
相关问题