From: Anthony Durity Date: 2006-02-07T01:28:15+09:00 Subject: Re: Merging regular expressions Thanks Robert, thanks Dave, I see what ye both mean. I was more thinking along the lines of a situation like this. To avoid iterating over the array, I would like to compress the regexps into a large regexp while still preserving the identity of each regexp within the meta-regexp, something akin to what happens when the scanner of a lexer is created. So, imagine this trivial situation... r[0] = /a/ r[1] = /b/ m = Regexp.union(r0.source, r1.source) # _but_ when I go to check s with m =~ s # if it matches, i want it to tell me which of the original r[i] would have matched! See what I mean? Will I hack the Ruby source? Can this be done in Ruby? Will I have to do it in C? Thanks a million in advance again... Anthony On 2/6/06, Robert Klemme wrote: > 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