2012-05-12 114 views
0
name="(this is 
a message 
) 

(other message)"; 

res=name.scan(/(\(.*\))/m); 
puts res.join("###"); 

在上面的代码中,我想提取与它内部文本匹配的括号作为整体表达。在红宝石中寻找匹配字符串的模式

我的意思是我想通过我的代码得到以下结果:

(this is 
a message 
)###(other message) 

这就是说,对资源的长度应为2而不是1, 但在我的代码,我总是得到:

(this is 
a message 
) 
(other message) 

我的模式一定有问题,任何人都可以帮我解决它吗?

回答

2

您想使用non-greedy比赛,所以正则表达式行更改为:

res=name.scan(/(\(.*?\))/m);