From: MikkelFJ Date: 2003-03-12T05:11:59+09:00 Subject: Re: scanning strings "MikkelFJ" wrote in message news:3e6e3970$0$149$edfadb0f@dtext01.news.tele.dk... > If not, wouldn't that be a useful feature in Ruby? I imagined something like string.scan2(/\s+/) |m| { process_text if m.pre_match; process_space if m[0] } Of course, what I would really like to have is: string.scan([:symbol1, Regexp1], [:symbol2, Regexp2], ...]) do |s, m| case s when :symbol1 # ... process m when :symbol2 # ... else # s == :default # ... end Initially I don't care how inefficient it is, but long term, this would allow the regex array to be compiled efficiently into a lexer. This would seriously kick ass. The match object would also be able to track linenumbers, such that the else branch could produce a sensible error message. I noticed Ruby 1.8 had named subexpression in regexp source, perhaps some of that could be used? For my original problem I could write. string.scan([:space, /\s+/]) |s, m| case s when :space process_space m[0] else process_text m[0] end but in this case it would be an overkill. Mikkel