5

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?

2
  • what array are you trying to get? [67, 67] ? Commented Feb 16, 2012 at 20:30
  • I wanted the whole array [56,67,67,45,56]. I can understand why you asked this. thanks! Commented Feb 16, 2012 at 21:06

2 Answers 2

11
string.scan(/regex/)

should do it

Sign up to request clarification or add additional context in comments.

Comments

6

scan does what you want:

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

Comments

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.