Match all occurrences of a regex?
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.
1 Expert Answer
I can see where the frustration comes from. Actually the method is not in the Regexp class, but rather in the String class:
From https://apidock.com/ruby/String/scan :
iterate through str, matching the pattern (which may be a Regexp or a String). For each match, a result is generated and either added to the result array or passed to the block.
"This is a string with many letters inside of it".scan(/is/)
=> ["is", "is"]
Hope this helps!
Still looking for help? Get the right answer, fast.
Find an Online Tutor Now
Choose an expert and meet online.
No packages or subscriptions, pay only for the time you need.