[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Regexp question for doing "and" searches

Jeff

2/3/2006 9:38:00 PM

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



--
Posted via http://www.ruby-....


4 Answers

Andrew Johnson

2/3/2006 9:58:00 PM

0

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

Joel VanderWerf

2/3/2006 10:03:00 PM

0

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

Seems to work:

s = "foo two bar three zap one"
rx = /^(?=.*one)(?=.*two).*three/m

p (rx =~ s)

s = "one three"
p (rx =~ s)

Works like this too:
rx = /^(?=.*one)(?=.*two)(?=.*three)/m

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Jeff

2/3/2006 10:08:00 PM

0

Andrew Johnson wrote:
>
> 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.
>

Awesome! I'll give it a try.

Thanks a lot.
Jeff

--
Posted via http://www.ruby-....


Ara.T.Howard

2/3/2006 10:17:00 PM

0