1

I want to be able to do a search in a set of documents on two words "country" and "vehicle"... I want to only see documents that have both words in them but once a document has both words, I want to hit on all occurrences of either word.

I've tried

(?=(country|vehicle)) 
(?= country )(?= vehicle)
( (country)* | (vehicle)*) | ( (country .* vehicle) )
(?=.*vehicle)(?=.*country)

I can't seem to get it just right, any suggestions?

2
  • (?=.*vehicle)(?=.*country) should work, what's the problem?
    – anubhava
    Commented Jul 13, 2015 at 16:15
  • I guess I'm just looking for confirmation since I'm using agent Ransack/ FileLocatorPro and there aren't any hits highlighted in the results. If I type something "less complex" all the matches are in blue. i don't get the blue when i see results Commented Jul 13, 2015 at 16:19

1 Answer 1

1

You need to use single line option and an anchor to speed up processing:

(?s)^(?=.*vehicle)(?=.*country)

If you need to match the words as whole words, use \b word boundary around them.

Without the singleline mode, the words you are checking for might not be reached as they might be located on the second, third etc. lines and the lookaheads will fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.