2012-05-12 111 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);