From: Robert Klemme Date: 2006-02-06T23:38:20+09:00 Subject: Re: Merging regular expressions Dave Burt wrote: > Anthony Durity asked: >> Imagine I have a bunch of n RegExps r[] - they could be any valid >> ruby Regexps. >> Say I want to match each of them in turn to a certain something s to >> make sure that no two Regexps in the list both match s. >> Now say that I want a meta regular expression m that is all the >> Regexps merged together so that the following pseudo code wroks, >> >> m = MetaRegExp.new >> for each r { [i] m.add(r[i]) } # add makes sure that no two RegExps >> would match the same input >> >> m =~ s # returns an int representing which of the n original RegExps >> would have matched > > Why not just use the array of regexps like this? > > matches = r.map{|i| r =~ s } > matches.index(matches.select{|i| i }[0]) # returns the int you're > after I guess you rather meant matches = r.map {|i| i =~ s} error = matches.compact.size != 1 Kind regards robert