[英]looking for a pattern in ruby for matching string
name="(this is
a message
)
(other message)";
res=name.scan(/(\(.*\))/m);
puts res.join("###");
In the above code,I want to extract the matched parenthesis with the text inside the it as a whole expresstion. 在上面的代码中,我想提取匹配的括号,并将其中的文本作为整体表达。
I mean I want to get the following result through my code: 我的意思是我希望通过我的代码获得以下结果:
(this is
a message
)###(other message)
That's to say,the length of the res should be 2 not 1, But in my code,I always get: 也就是说,res的长度应该是2而不是1,但在我的代码中,我总是得到:
(this is
a message
)
(other message)
There must be something wrong with my pattern,can any one help me to fix it? 我的模式一定有问题,任何人都可以帮我解决吗?
您想要使用非贪婪的匹配,因此将正则表达式行更改为:
res=name.scan(/(\(.*?\))/m);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.