简体   繁体   中英

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

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, eg 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

查找 -> 正则表达式,然后输入

(CAT)(.*?)(ROAD)

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.

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM