Maria Peter
Maria Peter

Reputation: 31

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

Upvotes: 1

Views: 475

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174844

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.

Upvotes: 4

Related Questions