Retrieving a pattern that appears multiple times in a string using ruby ​​regex

I want to extract patterns that appert several times in a row. For example, getting two arrays of two integers from a string

wahoaet56oihaioet67jlkiwoeah67ladohwae45lkaowearho56

I thought result="wahoaet56oihaioet67jlkiwoeah67ladohwae45lkaowearho56".match(/([0-9]{2})/) should provide a MatchData object, the captures method should give me an array of matching patterns, but something seems to be missing me. It returns only the first find. Even using $1,$2,$3 , etc. Does not work. I am using ruby

How should I do it?

+4
source share
2 answers
 string.scan(/regex/) 

gotta do it

+10
source

scan does what you want:

 str = "wahoaet56oihaioet67jlkiwoeah67ladohwae45lkaowearho56" p str.scan(/\d+/) #=> ["56", "67", "67", "45", "56"] 
+5
source

Source: https://habr.com/ru/post/1396917/


All Articles