Since your capture only matches a single character, one . (.)+
does not match (.+)
>> /(.)+?/.match("Many many characters!").captures => ["M"] >> /(.+)?/.match("Many many characters!").captures => ["Many many characters!"] >> /(.+?)/.match("Many many characters!").captures => ["M"]
If you want each character to match recursively, use String#scan
or String#split
if you don't need capture groups
Using scan:
"Many many characters!".scan(/./) #=> ["M", "a", "n", "y", " ", "m", "a", "n", "y", " ", "c", "h", "a", "r", "a", "c", "t", "e", "r", "s", "!"]
Note that the other answer uses (.)
, While it is fine if you care about the capture group, it is a little pointless if you do not, otherwise it will return EVERY CHARACTER to its own separate array, for example:
[["M"], ["a"], ["n"], ["y"], [" "], ["m"], ["a"], ["n"], ["y"], [" "], ["c"], ["h"], ["a"], ["r"], ["a"], ["c"], ["t"], ["e"], ["r"], ["s"], ["!"]]
Otherwise, just use split
: "Many many characters!".split(' ')"
EDIT In response to your edit:
reg = /([\w-\.]+)@((?:[\w]+\.)+)([a-zA-Z]{2,4})/ str = " elliotpotts@sample.com " str.scan(reg).flatten.map { |capture| [capture, str.index(capture), capture.size] } #=> [["elliotpotts", 0, 11], ["sample.", 12, 7], ["com", 19, 3]]`
Oh, and you don’t need to scan, you don’t actually scan, so you don’t need to go through at least not with the example you gave:
str.match(reg).captures.map { |capture| [capture, str.index(capture), capture.size] }
Will also work