How to match all occurrences of a regex

Chris Bunch picture Chris Bunch · Sep 17, 2008 · Viewed 195.8k times · Source

Is there a quick way to find every match of a regular expression in Ruby? I've looked through the Regex object in the Ruby STL and searched on Google to no avail.

Answer

Jean picture Jean · Sep 17, 2008

Using scan should do the trick:

string.scan(/regex/)