From: ara.t.howard@... Date: 2006-02-04T07:17:16+09:00 Subject: Re: Regexp question for doing "and" searches On Sat, 4 Feb 2006, Jeff Cohen wrote: > Just can't quite figure this one out. > > Given a search phrase like "one two three", I want to search a list of > text strings for onex that contain ALL of those words, but not > necessarily in that order. > > The hard part for me is the "not necessarily in that order". > > Using '\b(one|two|three)\b' will match if at least one of them occurs in > any order, but I need all of them to match *and* be in any order. > > two one three => match > one three => does not match > > Any ideas? Is this possible with regular expressions? > > Thanks! > Jeff stupid - but works harp:~ > cat a.rb strings = <<-txt two one three one three one foo two bar three foo two bar one three two txt atoms = '\b(?:one|two|three)\b' re = %r/(?:#{ atoms }).*(?:#{ atoms }).*(?:#{ atoms })/ require "yaml" and strings.each{|string| y string => (string =~ re ? true : false)} harp:~ > ruby a.rb --- " two one three\n": true --- " one three\n": false --- " one foo two bar three\n": true --- " foo two bar one three\n": true --- " two\n": false hth. -a -- happiness is not something ready-made. it comes from your own actions. - h.h. the 14th dali lama