Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting a pattern that appers multiple times in a string using ruby regex

Tags:

regex

ruby

I want to extract patterns that appert multiple times in a string. For example,getting two an array lay of two digit integers from a string

wahoaet56oihaioet67jlkiwoeah67ladohwae45lkaowearho56

I thought result="wahoaet56oihaioet67jlkiwoeah67ladohwae45lkaowearho56".match(/([0-9]{2})/) should give a MatchData object whose captures method should give me an array of matched patters, but it seems there is something I am missing. It only give back the first find. Even using $1,$2,$3 etc doesn't work. I am using ruby

How should I do this?

like image 501
serengeti12 Avatar asked Dec 06 '22 16:12

serengeti12


1 Answers

string.scan(/regex/)

should do it

like image 98
ohaal Avatar answered May 24 '23 07:05

ohaal