简体   繁体   中英

How to match multiple lines in ruby

This doesn't matches multiple "m"

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

Above code matches only first "m"

In perl usually i do

$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.

> 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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