Search and select lines containing all keywords in notepad++



  • Hi,
    I am looking for a regular expression or any option… that allows me to search and select lines from notepad++, that contain all of the keywords.

    Eg.
    The CAT and the DOG are friends
    The CAT is named Purr.
    The DOG is named Bark.
    It is raining CATs and DOGs.

    I need to select lines 1 and 4.

    Please help





  • @Alan-Kilborn
    Thanks for the link



  • Hello, @alan-kilborn, @maria-nita and All,

    Alan, the first answer of this stackoverflow’s topic is really the best one ;-))

    Indeed ! At first sight, although that, in OP text , word CAT always precedes the word DOG, I thought about the search regex, below :

    ^.*(CAT).*(DOG).*|^.*(?2).*(?1).*

    However, the syntax ^(?=.*CAT)(?=.*DOG).* is really more elegant !


    We can generalize to any number of words and, also, add conditions which should not occur ;-)) . For instance, the following regex would match all lines, with their line-breaks, which :

    • Contain the 3 expressions WORD_1 AND WORD_2 AND WORD_5

    • Does NOT contain the 2 expressions WORD_4 OR WORD_6

    (?-s)^(?=.*WORD_1)(?=.*WORD_2)(?=.*WORD_5)(?!.*WORD_4)(?!.*WORD_6).*\R

    So, it matches the lines 5, 12 and 14, only, in the text :

    This line   1  contains   WORD_1  WORD_2
    This line   2  contains   WORD_1  WORD_3  WORD_5
    This line   3  contains   WORD_3  WORD_5  WORD_4
    This line   4  contains   WORD_1  WORD_5
    This line   5  contains   WORD_5  WORD_2  WORD_1
    This line   6  contains   WORD_2  WORD_1  WORD_6  WORD_5
    This line   7  contains   WORD_4  WORD_1  WORD_5
    This line   8  contains   WORD_2
    This line   9  contains   WORD_5  WORD_2  WORD_4  WORD_7  WORD_1
    This line  10  contains   WORD_1  WORD_6  WORD_2
    This line  11  contains   WORD_4  WORD_2  WORD_1, WORD_5  WORD_6
    This line  12  contains   WORD_7  WORD_2  WORD_5  WORD_1
    This line  13  contains   WORD_6  WORD_2  WORD_1  WORD_5
    This line  14  contains   WORD_1  WORD_5  WORD_8  WORD_2  WORD_3
    

    Best Regards,

    guy038


Log in to reply