[英]looking for a pattern in ruby for matching string
name="(this is
a message
)
(other message)";
res=name.scan(/(\(.*\))/m);
puts res.join("###");
在上面的代码中,我想提取匹配的括号,并将其中的文本作为整体表达。
我的意思是我希望通过我的代码获得以下结果:
(this is
a message
)###(other message)
也就是说,res的长度应该是2而不是1,但在我的代码中,我总是得到:
(this is
a message
)
(other message)
我的模式一定有问题,任何人都可以帮我解决吗?
您想要使用非贪婪的匹配,因此将正则表达式行更改为:
res=name.scan(/(\(.*?\))/m);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.