From: Michal Suchanek Date: 2010-02-07T06:32:57+09:00 Subject: Re: match/scan does not return multiple matches On 6 February 2010 21:47, Rick DeNatale wrote: > On Sat, Feb 6, 2010 at 3:23 PM, Brian Candler wrote: >> Michal Suchanek wrote: > >>> "ajabcabck".scan /^a*j(?:b*(a+)b+c*)+k$/ >>> => [["a"]] >>> >>> >>> clearly the a+ group must match twice to match the string from ^ to $ >>> but only single match is returned. >> >> But the regular expression you're passing is anchored, so the entire >> regexp is only matched once, and it only contains one capturing group. > > Well I think that I understand what the OO is saying, let's break the > match down: > > "ajabcabck".match /^a*j(?:b*(a+)b+c*)+k$/ > >  /^a*j/  matches "aj" leaving "abcabck" > /(?:b*(a+)b+c*)+ matches "abcabc" leaving "k" > /k$/ matches "k" and we're done > > Now there's a capture group inside that second part a non-capture > group which can (and does in this case repeat). > > Since it repeats one might think that there would be one capture for > each repetition, but there isn't. Only the first actually gets > captured. > > Here's a simpler example: > > /^(a)+$/.match("aa").to_a > => ["aa", "a"] > > > Also see http://www.regular-expressions.info/captureall.html Thanks for the explanations. As mentioned on the page and also explained in Brian's reply this is a design limitation of the return value of the match method. It could return the additional matches but then the return value would have to be structured differently than it is now for the result to make sense. As scan most likely uses match internally or at least returns results consistent with match it shares the limitation. So something like split has to be used to slice the string into pieces where either a shorter non-anchored regex can match repeatedly or only one match can be found. The case which causes problems and is not actually well captured by the example is something like ab=cd,ef, ... where the regexes for 'ab', 'cd' and the rest are slightly different, and so is the interpretation. Thanks Michal