0

I got a string "[5, 3]" and I want to extract 5 and 3 from it using regex.

p "[5, 3]".match(/\d,\s\d/) # <MatchData "5, 3">
p "[5, 3]".match(/\d/) # <MatchData "5">

I can't extract both numbers at same time

Headmaster
  • 2,008
  • 4
  • 24
  • 51

1 Answers1

3

I think you need scan instead match, try:

p "[5, 3]".scan(/\d/) 

What I can say is match returns the first match as MatchData, while scan returns everything what the regex matches.

See String#scan and Regexp#match.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59