Boti
Boti

Reputation: 3435

ruby regexp find the pattern more than once

value = "Men - $30, Women - $20"
# value = "Men - 0"
# value = "free"
data = /\$(\d*)/.match value

Currently:

data.to_a
 => ["$30", "30"] 

I want:

data.to_a
 => ["$30", "30", "$20", "20] 

How to accomplish that

Upvotes: 0

Views: 1661

Answers (1)

falsetru
falsetru

Reputation: 369034

Using String#scan

value = "Men - $30, Women - $20"

value.scan(/(\$(\d+))/)
# => [["$30", "30"], ["$20", "20"]]

value.scan(/(\$(\d+))/).flatten
# => ["$30", "30", "$20", "20"]

Upvotes: 6

Related Questions