Andrew Johnson
2/3/2006 9:58:00 PM
On Sat, 4 Feb 2006 06:38:12 +0900, Jeff Cohen <cohen.jeff@gmail.com> 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.
You can use look-aheads to build a re that looks like:
/^(?=.*\bone\b)(?=.*\btwo\b)(?=.*\bthree\b)/
ensuring a match only if all look-ahead assertions pass.
phrase = "one two three"
re = %r/^#{phrase.split.map{|s|"(?=.*\\b#{s}\\b)"}}/
while DATA.gets
print if ~re
end
__END__
one two three YUP
one three two YUP
zone two three NOPE
three and two and one YUP
regards,
andrew