Martin Pirker wrote:
> Imagine an input string
> aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee...
> 
> I have regexp for the parts a,b,c
> and e can be considered as else.
> 
> So how can I efficiently search/step through the string from left to
> right, while calling for each section the fitting handler, kind of
> 
> case section
>   /aaaa/ ...
> 
>   /bbb/  ...
> 
>   /cccc/ ..
> 
>   else
> 
> end

You are pretty close:

>> s='aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee'
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee"
>> s.scan /a+|b+|c+/ do |m|
?>   case m
>>     when /a+/
>>       puts "A"
>>     when /b+/
>>       puts "B"
>>     when /c+/
>>       puts "C"
>>   end
>> end
A
B
C
B
A
B
A
C
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee"

Or, if you want to avoid a second match:

>> s.scan /(a+)|(b+)|(c+)/ do |m|
?>   case ( m.inject(0) {|i,e| break i if e; i + 1} )
>>     when 0
>>       puts "A"
>>     when 1
>>       puts "B"
>>     when 2
>>       puts "C"
>>   end
>> end
A
B
C
B
A
B
A
C
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceeee"
>>

Kind regards

    robert