简体   繁体   English

如何匹配ruby中的多行

[英]How to match multiple lines in ruby

This doesn't matches multiple "m" 这与多个“m”不匹配

a = "Im the prowerful man"
puts a.match(/(m)/im)[1]

Above code matches only first "m" 以上代码仅匹配第一个“m”

In perl usually i do 在perl中我通常会这样做

$a =~ m/(m)/sig

How to do similarly in ruby 如何在红宝石中做同样的事情

Use string.scan instead of string.match where the match function would return only the first match. 使用string.scan而不是string.match ,其中match函数将仅返回第一个匹配项。

> a = "Im the prowerful man"
> a.scan(/m/im)
=> ["m", "m"]
> a.scan(/(m)/im)
=> [["m"], ["m"]]

Multidimensional array at the output is because of the capturing group present in your regex. 输出中的多维数组是因为正则表达式中存在捕获组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM