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