From: Martin Pirker Date: 2005-12-14T22:22:40+09:00 Subject: Re: string mangling Robert Klemme 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 [...] > Or, if you want to avoid a second match: of course I want :-) >>> 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" ....but this doesn't allow a processing step in the "else" case? Martin