fifamaniac04
fifamaniac04

Reputation: 2383

Regular expression AND not matching correctly

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?

Upvotes: 1

Views: 94

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

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.

Upvotes: 1

Related Questions