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.
相关问题
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
- reference to a method?
- ruby 1.9 wrong file encoding on windows
- gem cleanup shows error: Unable to uninstall bundl
相关文章
- Ruby using wrong version of openssl
- Optimization techniques for backtracking regex imp
- Regex to check for new line
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- “No explicit conversion of Symbol into String” for
- Segmentation fault with ruby 2.0.0p247 leading to
- How to detect if an element exists in Watir
Using
scan
should do the trick:if you have a regexp with groups :
You use scan of string method to find matching groups:
To find matching pattern:
For finding all the matching strings, Use
scan
method ofString
class.If you rather want
MatchData
which is the type of the object returned by,match
method ofRegexp
classs, use the followingThe benefit of having
MatchData
is that you can use methods likeoffset
Refer these questions too if you'd like to know more
How do I get the match data for all occurrences of a Ruby regular expression in a string?
Ruby regular expression matching enumerator with named capture support
How to find out the starting point for each match in ruby
Reading about special variables
$&
,$'
,$1
,$2
in ruby will be super helpful.