James Gray
12/14/2005 1:39:00 PM
On Dec 14, 2005, at 6:32 AM, Simon Strandgaard wrote:
> On 12/14/05, Martin Pirker <crf@sbox.tu-graz.ac.at> 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
>
> irb(main):001:0> s = 'aaaaabbccccceeebbbbbbbbbbeaaabaacccceee'
> => "aaaaabbccccceeebbbbbbbbbbeaaabaacccceee"
> irb(main):002:0> s.scan(/(a+)|(b+)|(c+)|([^abc]+)/)
> => [["aaaaa", nil, nil, nil], [nil, "bb", nil, nil], [nil, nil,
> "ccccc", nil], [nil, nil, nil, "eee"], [nil, "bbbbbbbbbb", nil, nil],
> [nil, nil, nil, "e"], ["aaa", nil, nil, nil], [nil, "b", nil, nil],
> ["aa", nil, nil, nil], [nil, nil, "cccc", nil], [nil, nil, nil,
> "eee"]]
> irb(main):003:0>
My similar thought:
>> str = 'aaaaabbccccceeebbbbbbbbbbeaaabaacccceee'
=> "aaaaabbccccceeebbbbbbbbbbeaaabaacccceee"
>> str.scan(/((\w)\2*)/).map { |chunk| chunk.first }
=> ["aaaaa", "bb", "ccccc", "eee", "bbbbbbbbbb", "e", "aaa", "b",
"aa", "cccc", "eee"]
James Edward Gray II