ihtus
ihtus

Reputation: 2811

Notepad++ search with a logical AND

Using Notepad++ I need to find lines that would contain 2 keywords (both).

I've found how to combine 2 regex with a logical 'or' operator.

Example: (searchword1)|(searchword2)

But how do I combine with logical 'and'?

Tried &, &&.. no success.

Example of input:

The CAT goes up and down the ROAD.
The CAT goes up and down the CITY.

Search words: CAT & ROAD

Expected result: line1

Upvotes: 5

Views: 22821

Answers (4)

tom redfern
tom redfern

Reputation: 31780

Just an addition to @acarlon answer, which is the one that worked for me.

To make this work in notepad++ you must make sure you have selected the "matches newline" checkbox as well as the Regular expression mode:

enter image description here

Upvotes: 0

AdrianHHH
AdrianHHH

Reputation: 14076

A simple pattern if only two words are wanted would use the or operator to search for the words in either order:

(CAT.*ROAD)|(ROAD.*CAT)

For more than two words the positive lookahead is probably better.

Upvotes: 0

acarlon
acarlon

Reputation: 17272

If you are looking for a true && operation where a line contains both words in any order then you will want to match both of these lines:

The CAT goes up and down the ROAD.

The ROAD goes up and down the CAT. (poor cat)

In this case you will want to use:

^(?=.*\bCAT\b)(?=.*\bROAD\b).*$

Explanation:

  • ^ start line
  • $ end line
  • ?= positive look ahead
  • \b word boundary. Not sure if you want this or not. Remove these if you want to match any part of word, e.g. TheCATgoes up and down theROAD.

(?=) is a positive look ahead. We have two such look aheads, one for anything (*) followed by CAT and one for anything (*) followed by ROAD. There is an implied && between the two lookaheads - both conditions must be satisfied.

Read up on lookarounds here

Upvotes: 9

LorDex
LorDex

Reputation: 2636

Find -> Regular expression, then type

(CAT)(.*?)(ROAD)

Upvotes: 8

Related Questions