0
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

3
  • just enable the global g modifier. Commented Jul 17, 2014 at 14:51
  • 2
    That is: unknown regexp option - g Commented Jul 17, 2014 at 14:54
  • 1
    If you don’t need repetitions, here you go: "Men - $30, Women - $20".scan(/\$(\d*)/). Commented Jul 17, 2014 at 14:55

1 Answer 1

6

Using String#scan

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

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

value.scan(/(\$(\d+))/).flatten
# => ["$30", "30", "$20", "20"]
Sign up to request clarification or add additional context in comments.

1 Comment

Short and sweet, I like the flatten :) +1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.