On 13.08.2010 00:50, Iain Barnett wrote:
>
> On 12 Aug 2010, at 19:52, Philipp Kempgen wrote:
>
>> Philipp Kempgen wrote:
>>> I'm looking for a parser which can be fed with some (A)BNF-style rules.
>> ...
>>> If so, which parser is more appropriate?
>>> It should hand back a parse tree.
>>
>> Alternatively I'd appreciate if Regexps could return *all*
>> occurrences of named capture groups inside repetitions etc.
>> instead of just the last match for each name. Feasible?

As I understand you want /f(o)+/ =~ "foo" to return ["o", "o"] as match 
for the group (used normal capturing groups for simplicity).

> I was trying to do the same thing and asked about repeated named captures.
> http://osdir.com/ml/ruby-talk/2010-07/msg00361.html
>
> It seems that using String#scan is the closest anything Ruby has, as the Oniguruma regex engine doesn't support it. I think it's a real shame as named capture groups are really useful.

Regular expression engines generally do only return one match per group 
- regardless of naming or not naming groups.  I'm not up to date with 
current Perl's regular expressions which are the only ones I can imagine 
to be crazy enough to provide such a feature. :-)  Otherwise String#scan 
is indeed the proper tool to get multiple matches.  The example above 
could be done like this

if /f(o+)/ =~ s
   $1.scan /o/ do |match|
     p match
   end
end

or this

if /f(o+)/ =~ s
   matches = $1.scan /o/
   p matches
end

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/